C++ Programs Example List 2
Simple inheritance Example
class Shape {
protected:
int width;
int height;
public:
void setWidth(int w) {
width = w;
}
void setHeight(int h) {
height = h;
}
};
// Derived class
class Rectangle:
public Shape {
public:
int getArea() {
return (width * height);
}
};
int main(void) {
Rectangle Rect;
Rect.setWidth(5);
Rect.setHeight(7);
// Print the area of the object.
cout << "Total area: "
<< Rect.getArea() << endl;
return 0;
}
Multilevel inheritance Example
#include
<iostream>
#include
<conio.h>
using namespace std;
class person
{
char name[100],gender[10];
int age;
public:
void getdata()
{
cout<<"Name: ";
fflush(stdin); /*clears input stream*/
gets(name);
cout<<"Age: ";
cin>>age;
cout<<"Gender: ";
cin>>gender;
}
void display()
{
cout<<"Name:
"<<name<<endl;
cout<<"Age:
"<<age<<endl;
cout<<"Gender:
"<<gender<<endl;
}
};
class employee:
public person
{
char company[100];
float salary;
public:
void getdata()
{
person::getdata();
cout<<"Name of Company:
";
fflush(stdin);
gets(company);
cout<<"Salary:
Rs.";
cin>>salary;
}
void display()
{
person::display();
cout<<"Name of Company:
"<<company<<endl;
cout<<"Salary:
Rs."<<salary<<endl;
}
};
class programmer:
public employee
{
int number;
public:
void getdata()
{
employee::getdata();
cout<<"Number of
programming language known: ";
cin>>number;
}
void display()
{
employee::display();
cout<<"Number of
programming language known: "<<number;
}
};
int main()
{
programmer p;
cout<<"Enter
data"<<endl;
p.getdata();
cout<<endl<<"Displaying
data"<<endl;
p.display();
getch();
return 0;
}
Output:
Enter data
Name: Piyush Patel
Age: 33
Gender: Male
Name of Company: Infosys
Salary: $21000
Number of
programming language known: 4
Displaying data
Name: Piyush Patel
Age: 33
Gender: Male
Name of Company: Infosys
Salary: $21000
Number of
programming language known: 4
Multiple inheritance Example
#include
<iostream>
// 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();
// Print the area of the object.
cout << "Total area: "
<< Rect.getArea() << endl;
// Print the total cost of painting
cout << "Total paint cost:
$" << Rect.getCost(area) << endl;
return 0;
}
Function Overloading Example in C++
#include <iostream>
class printData {
public:
void print(int i) {
cout <<
"Printing int: " << i << endl;
}
void print(double f) {
cout <<
"Printing float: " << f << endl;
}
void print(char* c) {
cout <<
"Printing character: " << c << endl;
}
};
int main(void) {
printData pd;
// Call print to print integer
pd.print(5);
// Call print to print float
pd.print(500.263);
// Call print to print
character
pd.print("Hello
C++");
return 0;
}
Output:
Printing
int: 5
Printing
float: 500.263
Printing
character: Hello C++
Operators Overloading Example in C++
#include <iostream>
class Box {
private:
double length; // Length of a box
double breadth; //
Breadth of a box
double height; // Height of a 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 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;
}
};
// Main function for the program
int main() {
Box Box1; // Declare Box1 of type Box
Box Box2; // Declare Box2 of type Box
Box Box3; // Declare Box3 of type Box
double volume = 0.0; // Store the volume of a box here
// box 1 specification
Box1.setLength(6.0);
Box1.setBreadth(7.0);
Box1.setHeight(5.0);
// box 2 specification
Box2.setLength(12.0);
Box2.setBreadth(13.0);
Box2.setHeight(10.0);
// volume of box 1
volume = Box1.getVolume();
cout << "Volume of
Box1 : " << volume <<endl;
// volume of box 2
volume = Box2.getVolume();
cout << "Volume of
Box2 : " << volume <<endl;
// Add two object as follows:
Box3 = Box1 + Box2;
// volume of box 3
volume = Box3.getVolume();
cout << "Volume of
Box3 : " << volume <<endl;
return 0;
}
Output:
Volume of Box1 : 210
Volume of Box2 : 1560
Volume of Box3 : 5400
Comments
Post a Comment