In C++ REQUIREMENTS: Problem Description: Objective: Create Inheritance Hierarch
ID: 3818156 • Letter: I
Question
In C++
REQUIREMENTS:
Problem Description:
Objective: Create Inheritance Hierarchy to Demonstrate Polymorphic Behavior
Create a Rectangle Class
Derive a Square Class from the Rectangle Class
Derive a Right Triangle Class from the Rectangle Class
Create a Circle Class
Create a Sphere Class
Create a Prism Class
Define any other classes necessary to implement a solution
Define a Program Driver Class for Demonstration
Create a Container to hold objects of the types described above
Create and Add two(2) objects of each type described above
Compute the area, perimeter, and volume in a uniform manner for each object in the Container. Display the results. Note: perimeter is not defined for a 3D object and volume is not defined for a 2D object.
Document your code, tell me what you are doing, and make it readable. Creativeness and Quality Craftsmanship do count.
Explanation / Answer
Inheritence :
One of the most important concepts in object-oriented programming is that of inheritance. Inheritance allows us to define a class in terms of another class, which makes it easier to create and maintain an application. This also provides an opportunity to reuse the code functionality and fast implementation time.
When creating a class, instead of writing completely new data members and member functions, the programmer can designate that the new class should inherit the members of an existing class. This existing class is called the base class, and the new class is referred to as the derived class.
The idea of inheritance implements the is a relationship. For example, mammal IS-A animal, dog IS-A mammal hence dog IS-A animal as well and so on.
Base & Derived Classes
A class can be derived from more than one classes, which means it can inherit data and functions from multiple base classes. To define a derived class, we use a class derivation list to specify the base class(es). A class derivation list names one or more base classes and has the form:
class derived-class: access-specifier base-class
Where access-specifier is one of public, protected, or private, and base-class is the name of a previously defined class. If the access-specifier is not used, then it is private by default.
Consider a base class Shape and its derived class Rectangle as follows:
Shape.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef SHAPE_H
#define SHAPE_H
class Shape{ // This is the base class!
public:
Shape();
float get_area();
private:
float area, perimeter;
};
#endif
Shape.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "Shape.h"
#include "Triangle.h"
#include "Rectangle.h"
#include "Square.h"
#include <iostream>
using namespace std;
Shape::Shape(){
area = 0;
perimeter = 0;
}
float Shape::get_area(){
return area;
}
Triangle.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef TRIANGLE_H
#define TRIANGLE_H
class Triangle : public Shape{
public:
Triangle(float base, float height);
float get_area();
private:
float base, height;
};
#endif
Triangle.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "Shape.h"
#include "Triangle.h"
#include "Rectangle.h"
#include "Square.h"
#include <iostream>
using namespace std;
Triangle::Triangle(float base, float height){
base = base;
height = height;
}
float Triangle::get_area(){
return (0.5*base*height);
}
Rectangle.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef RECTANGLE_H
#define RECTANGLE_H
class Rectangle : public Shape{
public:
Rectangle(float width, float length);
float get_area();
float get_perimeter();
private:
float width, length;
};
#endif
Rectangle.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "Shape.h"
#include "Triangle.h"
#include "Rectangle.h"
#include "Square.h"
#include <iostream>
using namespace std;
Rectangle::Rectangle(float width, float length){
width = width;
length = length;
}
float Rectangle::get_area(){
return width*length;
}
float Rectangle::get_perimeter(){
return 2*(length+width);
}
Square.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#ifndef SQUARE_H
#define SQUARE_H
class Square : public Rectangle{
public:
Square(float side);
private:
};
#endif
Square.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
#include "Shape.h"
#include "Triangle.h"
#include "Rectangle.h"
#include "Square.h"
#include <iostream>
using namespace std;
Square::Square(float side){
Rectangle r(side,side);
}
main.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "Shape.h"
#include "Rectangle.h"
#include "Triangle.h"
#include "Square.h"
#include <iostream>
using namespace std;
int main(){
Rectangle rectangleObject(2,5);
Triangle triangleObject(1,10);
Square squareObject(5);
cout << "The area of the rectangle is" << rectangleObject.get_area() << ' ';
cout << "The perimeter of the rectangle is " << rectangleObject.get_perimeter() << ' ';
cout << "The area of the triangle is " << triangleObject.get_area() << ' ';
cout << "The area of the square is " << squareObject.get_area() << ' ';
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef SHAPE_H
#define SHAPE_H
class Shape{ // This is the base class!
public:
Shape();
float get_area();
private:
float area, perimeter;
};
#endif