Part 2 object oriented
Structure 8 inheritance, overload, polymorphism, virtual function
Catalog
1 inheritance
#include <iostream>
using namespace std;
// Base class
class Shape
{
public:
void setWidth(int w)
{
width = w;
}
void setHeight(int h)
{
height = h;
}
protected:
int width;
int height;
};
// Derived class
class Rectangle: public Shape
{
public:
int getArea()
{
return (width * height);
}
};
int main(void)
{
Rectangle Rect;
Rect.setWidth(5);
Rect.setHeight(7);
// Area of output object
cout << "Total area: " << Rect.getArea() << endl;
return 0;
}
When the above code is compiled and executed, it produces the following results:
Total area: 35
#include <iostream>
using namespace std;
// Base class Shape
class Shape
{
public:
void setWidth(int w)
{
width = w;
}
void setHeight(int h)
{
height = h;
}
protected:
int width;
int height;
};
// Base class PaintCost
class PaintCost
{
public:
int getCost(int area)
{
return area * 70;
}
};
// Derived class
class Rectangle: public Shape, public PaintCost
{
public:
int getArea()
{
return (width * height);
}
};
int main(void)
{
Rectangle Rect;
int area;
Rect.setWidth(5);
Rect.setHeight(7);
area = Rect.getArea();
// Area of output object
cout << "Total area: " << Rect.getArea() << endl;
// Total output cost
cout << "Total paint cost: $" << Rect.getCost(area) << endl;
return 0;
}
When the above code is compiled and executed, it produces the following results:
Total area: 35
Total paint cost: $2450
#include <iostream>
using namespace std;
//Base class
class D
{
public:
D(){cout<<"D()"<<endl;}
~D(){cout<<"~D()"<<endl;}
protected:
int d;
};
class B:virtual public D
{
public:
B(){cout<<"B()"<<endl;}
~B(){cout<<"~B()"<<endl;}
protected:
int b;
};
class A:virtual public D
{
public:
A(){cout<<"A()"<<endl;}
~A(){cout<<"~A()"<<endl;}
protected:
int a;
};
class C:public B, public A
{
public:
C(){cout<<"C()"<<endl;}
~C(){cout<<"~C()"<<endl;}
protected:
int c;
};
int main()
{
cout << "Hello World!" << endl;
C c; //D, B, A ,C
cout<<sizeof(c)<<endl;
return 0;
}
Hello World!
D()
B()
A()
C()
40
~C()
~A()
~B()
~D()
2 overloaded operators and overloaded functions
#include <iostream>
using namespace std;
class printData
{
public:
void print(int i) {
cout << "Integer is: " << i << endl;
}
void print(double f) {
cout << "Floating point numbers are: " << f << endl;
}
void print(string c) {
cout << "The string is: " << c << endl;
}
};
int main(void)
{
printData pd;
// Output integer
pd.print(5);
// Output floating point number
pd.print(500.263);
// Output string
pd.print("Hello C++");
return 0;
}
When the above code is compiled and executed, it produces the following results:
The integer is: 5
Floating point: 500.263
The string is: Hello C++
#include <iostream>
using namespace std;
class Box
{
public:
double getVolume(void)
{
return length * breadth * height;
}
void setLength( double len )
{
length = len;
}
void setBreadth( double bre )
{
breadth = bre;
}
void setHeight( double hei )
{
height = hei;
}
// Overload + operator, used to add two Box objects
Box operator+(const Box& b)
{
Box box;
box.length = this->length + b.length;
box.breadth = this->breadth + b.breadth;
box.height = this->height + b.height;
return box;
}
private:
double length; // length
double breadth; // width
double height; // height
};
// Main function of program
int main( )
{
Box Box1; // Declare Box1, type Box
Box Box2; // Declare Box2 of type Box
Box Box3; // Declare Box3, type Box
double volume = 0.0; // Store volume in this variable
// Box1 details
Box1.setLength(6.0);
Box1.setBreadth(7.0);
Box1.setHeight(5.0);
// Box2 details
Box2.setLength(12.0);
Box2.setBreadth(13.0);
Box2.setHeight(10.0);
// Volume of Box1
volume = Box1.getVolume();
cout << "Volume of Box1 : " << volume <<endl;
// Volume of Box2
volume = Box2.getVolume();
cout << "Volume of Box2 : " << volume <<endl;
// Add the two objects to get Box3
Box3 = Box1 + Box2;
// Volume of Box3
volume = Box3.getVolume();
cout << "Volume of Box3 : " << volume <<endl;
return 0;
}
When the above code is compiled and executed, it produces the following results:
Volume of Box1 : 210
Volume of Box2 : 1560
Volume of Box3 : 5400
Unary operator overload
#include <iostream>
using namespace std;
class Distance
{
private:
int feet; // 0 to infinity
int inches; // 0 to 12
public:
// Required constructor
Distance(){
feet = 0;
inches = 0;
}
Distance(int f, int i){
feet = f;
inches = i;
}
// How to display distance
void displayDistance()
{
cout << "F: " << feet << " I:" << inches <<endl;
}
// Overload negative operator (-)
Distance operator- ()
{
feet = -feet;
inches = -inches;
return Distance(feet, inches);
}
};
int main()
{
Distance D1(11, 10), D2(-5, 11);
-D1; // Take the opposite number
D1.displayDistance(); // Distance D1
-D2; // Take the opposite number
D2.displayDistance(); // Distance D2
return 0;
}
When the above code is compiled and executed, it produces the following results:
F: -11 I:-10
F: 5 I:-11
Binary operator overload
Calculate the volume of Box3 above (the same code)
3 polymorphism and virtual function
#include <iostream>
using namespace std;
class Shape {
protected:
int width, height;
public:
Shape( int a=0, int b=0)
{
width = a;
height = b;
}
int area()
{
cout << "Parent class area :" <<endl;
return 0;
}
};
class Rectangle: public Shape{
public:
Rectangle( int a=0, int b=0):Shape(a, b) { }
int area ()
{
cout << "Rectangle class area :" <<endl;
return (width * height);
}
};
class Triangle: public Shape{
public:
Triangle( int a=0, int b=0):Shape(a, b) { }
int area ()
{
cout << "Triangle class area :" <<endl;
return (width * height / 2);
}
};
// Main function of program
int main( )
{
Shape *shape;
Rectangle rec(10,7);
Triangle tri(10,5);
// Address of the storage rectangle
shape = &rec;
// Calling area function of rectangle
shape->area();
// Storage triangle address
shape = &tri;
// Call area function of triangle
shape->area();
return 0;
}