Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Part 5 - Inheritance Step 1 - DEFINE 1) Create this class first: Square two

ID: 3603464 • Letter: C

Question

C++

Part 5 - Inheritance

Step 1 - DEFINE

1) Create this class first: Square

     two variables: length, width

     five function: getWidth, setWidth, getLength, setLength, getArea

2) Write the class second: Cube

     inheritance the square class code

     one variable: height

     three functions: getHeight, setHeight, getVolume

Step 2 - DECLARE 3 different instances of a cube.

Step 3 - USE the three instances of the cube.

     set the length, width and height of each

     get volume of each cube

Explanation / Answer

# EXAMPLE.CPP

#include <iostream>

using namespace std;

// Base class Square
class Square {
  
//member functio defination part
public:
//getters and setters
void setWidth(double w) {
width = w;
}
void setLength(double l) {
length = l;
}
double getWidth() {
return width;
}
double getLength(double l) {
return length;
}
//return square area
double getArea() {
return (width * length);
}
//varible decalration part  
protected:
double width;
double length;
};

// Derived class Cube
class Cube: public Square {
//member functio defination part
public:
//getters and setters
void setHeight(double h) {
height = h;
}
double getHeight() {
return height;
}
//return cube area
double getArea() {
return (width * length*height);
}
//varible decalration part  
protected:
double height;
};
//main defination
int main(void) {
  
Cube c1,c2,c3;
//set width ,length, height for instance c1 of Cube
c1.setWidth(5.1);
c1.setLength(7.2);
c1.setHeight(10.5);

//set width ,length, height for instance c2 of Cube
c2.setWidth(1);
c2.setLength(2);
c2.setHeight(5);

//set width ,length, height for instance c3 of Cube
c3.setWidth(4);
c3.setLength(3.2);
c3.setHeight(0.5);

// Print the area of the object c1.
cout << "Total area of c1 instance: " << c1.getArea() << endl;

// Print the area of the object c2.
cout << "Total area c2 instance: " << c2.getArea() << endl;

// Print the area of the object c3.
cout << "Total area c3 instance : " << c3.getArea() << endl;

return 0;
}

OUTPUT :

Total area of c1 instance: 385.56
Total area c2 instance: 10
Total area c3 instance : 6.4

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote