Polymorphism & Inheritance Programs

*******************************PROGRAM 1  ******************************

//Use of complex to demonstrate operator overloading

#include<iostream.h>
 #include<conio.h>
 class complex

{

int a,b,c;

public:

complex()

{

a=0;

b=0;

}

void getvalue()

{

cout<<"Enter the Two Numbers:";

cin>>a>>b;

}

//overloaded unary increment operator
 void operator++()

{

a=++a;

b=++b;

}

//overloaded unary decrement operator

void operator--()

{

a=--a;

b=--b;

}

//Display Complex number

void display()

{

cout<<a<<"+\t"<<b<<"i"<<endl;

}

};

int main()

{

complex obj;

obj.getvalue();

obj++;

cout<<"Increment Complex Number\n";

obj.display();

obj--;

cout<<"Decrement Complex Number\n";

obj.display();

return 0;

}

*******************************PROGRAM 2  ******************************

// Time class to demonstrate operator overloading and this pointer

#include<iostream>
 #include<stdlib>
 using namespace std;
 class Time
 {
 private:
 int hr,min,sec;

public:

Time();
 Time(int,int,int);
 Time(Time &);
 ~Time();
 void getData();
 void putData();
 Time operator +(Time);
 Time operator -(Time);
 Time operator --();

};
 Time::Time()
 {
 hr=min=sec=0;
 }

//Parameterized constructor using this pointer
 Time::Time(int hr,int min,int sec)
 {

this->hr=hr;
 this->min=min;
 this->sec=sec;

}

//Copy constructor using this pointer
 Time::Time(Time &t)
 {

this->hr=t.hr;
 this->min=t.min;
 this->sec=t.sec;

}

//Destructor
 Time::~Time()
 {
 hr=min=sec=0;
 }

//Read time in hr,min and sec
 void Time::getData()
 {

cout<<"Enter Time in hr.min,sec";
 cin>>hr>>min>>sec;

}

//Display time
 void Time::putData()
 {
 cout<<"\nTime is "<<hr<<":"<<min<<":"<<sec;
 }

//Overloading binary addition operator for time class
 Time Time::operator +(Time t)
 {

Time temp;
 temp.sec=sec+t.sec;
 temp.min=temp.sec/60;
 temp.sec=temp.sec%60;
 temp.min=temp.min+min+t.min;
 temp.hr=temp.min/60;
 temp.min=temp.min%60;
 temp.hr=temp.hr+hr+t.hr;
 return temp;

}

//Overloading binary minus operator for time class
 Time Time::operator -(Time t)
 {

Time temp;
 temp.sec=sec-t.sec;
 temp.min=min-t.min;
 temp.hr=hr-t.hr;
 return temp;

}

//Overloading unary decrement operator for time class
 Time Time::operator --()
 {

hr--;
 min--;
 sec--;
 return *this;

}




*******************************PROGRAM 3  ******************************

//Complex class to demonstrate overloading of >> and << operator 

#include<iostream>
 using namespace std;
 class Complex
 {
 private:
 float real,img;

public:

Complex(); //Default Constructor
 Complex(float r,float i);//Parameterized constructor
 Complex(Complex &);//copy constructor
 ~Complex(); //Destructor

friend ostream &operator<<(ostream &output , Complex &c)
 {

output << "Real : " << c.real << " Imag : " <<c.img;
 return output;

}

friend istream &operator>>( istream &input, Complex &c )
 {

input >> c.real >> c.img;
 return input;

}
 void show();
 void getData();
 };

Complex::Complex()
 {
 real=0.0;
 img=0.0;
 }
 Complex::~Complex()
 {

cout<<"in destructor";
 real=0.0;
 img=0.0;

}

Complex::Complex(float r,float i)
 {

real=r;
 img=i;

}
 Complex::Complex(Complex & c)
 {

real=c.real;
 img=c.img;

}
 Complex Complex::operator + (Complex c)
 {

Complex temp;
 temp.real=real+c.real;
 temp.img=img+c.img;
 return temp;

}

//Show complex number
 void Complex::show()
 {
 cout<<"\nComplex number is "<<real<<"+"<<img<<"i\n";
 }

//Read complex number
 void Complex::getData()
 {

cout<<"Enter real and img part of complex number\n";
 cin>>real>>img;

}

int main()
 {
 Complex c1,c2;
 cin>>c1>>c2;
 cout<<c1<<c2;
 return 0;
 }

*******************************PROGRAM 4  ******************************

//Complex class to demonstrate explicit and implicit call to constructor and 
//specify default values for parameters of function

#include<iostream>
 using namespace std;
 class Complex
 {
 private:
 float real,img;

public:

Complex(); //Default Constructor
 Complex(float r,float i);//Parameterized constructor
 Complex(Complex &);//copy constructor
 ~Complex();
 Complex operator +(Complex c);
 void show();
 void getData();

};
 Complex::Complex()
 {
 real=0.0;
 img=0.0;
 }
 Complex::~Complex()
 {
 cout<<"in destructor";
 real=0.0;
 img=0.0;
 }

Complex::Complex(float r=1,float i=10.3)
 {
 real=r;
 img=i;

}
 Complex::Complex(Complex & c)
 {
 real=c.real;
 img=c.img;
 }
 Complex Complex::operator + (Complex c)
 {
 Complex temp;
 temp.real=real+c.real;
 temp.img=img+c.img;
 return temp;
 }
 void Complex::show()
 {
 cout<<"\nComplex number is "<<real<<"+"<<img<<"i\n";
 }
 void Complex::getData()
 {
 cout<<"Enter real and img part of complex number\n";
 cin>>real>>img;
 }
 int main()
 {

Complex c1;
 c1.getData();
 c1.show();
 Complex c2(10.23);
 c2.show();
 Complex c3(c2);      //Implicit call to copy constructor
 Complex c4=c1;       //Explicit call to copy constructor
 c3.show();
 c3.getData();
 c3.show();
 c4.show();
 Complex c5;
 c5=c1+c2;
 return 0;

}

*******************************PROGRAM 5  ******************************

//Demonstration of single inheritance using student and account class.
/* Note That instead of char *, string class is used. 
string class is available in std namespace */

#include<string.h>
 #include<iostream>
 using namespace std;
 class Student
 {
 private :

//Data member declaration

int rollno;
 string name,dob;
 protected:
 float fees, per;
 int grno;

public:
 Student()  //Default constructor
 {

rollno=0;
 fees=0;
 grno=0;
 per=0;
 name="";
 dob="";

}
 Student(int r, string n,string d,float f,float p,int gr)
  //Parameterized constructor
 {

rollno=r;
 fees=f;
 grno=gr;
 per=p;
 name=n;
 dob=d;

}

//Mehod to display values of data members

void showData()
 {

cout<<"\nGRNO:"<<grno;
 cout<<"\tName:"<<name;
 cout<<"\nRollno:"<<rollno;
 cout<<"\tPer:"<<per;
 cout<<"\nDOB:"<<dob;
 cout<<"\tFees:"<<fees;

}
 };
 class Account:public Student 
//Single inheritance using public access specifier
 {
 protected:

float busfees;
 float tot;

public:
 Account():Student()
 {

busfees=0;
 tot=0;

}

//Inheriting parent class constructor

Account(int r,string n,string d,float f, float p,int gr,int b):Student(r,n,d,f,p,gr)
 {
 busfees=b;
 }
 void totfees()
 {
 tot=busfees+fees;
 }

void showData() //Overrided method
 {

Student::showData();  //Call showData method of Parent Class
 cout<<"\nBus Fees:"<<busfees;
 cout<<"\tTotal Fees:"<<tot<<endl;

}

};
 int main()
 {

Account a(1,"abc","1/2/3",60000,65.5,1234,20000);
 a.totfees();
 a.showData();
 return 0;

}

*******************************PROGRAM 6  ******************************

//Overriding demo using Multilevel Inheritance

#include<iostream>
 using namespace std;
 class A
 {
 private :
 int a;
 public:
 A()
 {
 a=0;
 }
 A(int a)
 {
 this->a=a;
 }
 void print()
 {
 cout<<"\nA="<<a;

}
 };
 class B:public A  //Derived class B from Base class A
 {
 private:
 int b;
 public:
 B():A()
 {
 b=0;
 }
 B(int a,int b):A(a)
 {
 this->b=b;
 }
 void print()
 { A::print();
 cout<<"\nB="<<b;
 }
 };
 class C: public B //Derived class C from Base class B
 {

int c;
 public:
 C():B()
 {
 c=0;
 }
 C(int a, int b,int c):B(a,b)
 {
 this->c=c;
 }
 void print()
 {
 B::print();
 cout<<"\nC="<<c;
 }
 };
 int main()
 {
 C c1;
 c1.print();

return 0;
 }

*******************************PROGRAM 7  ******************************

//Hierarchical Inheritance Demo

#include<string.h>
 #include<iostream>
 using namespace std;

//Base class Student

class Student
 {
 private :
 int rollno;
 char *name,*dob;
 protected:
 float fees, per;
 int grno;
 public:
 Student() //Default constructor
 {

rollno=0;
 fees=0;
 grno=0;
 per=0;
 name=new char[1];
 dob=new char[1];

}
 Student(int r, char *n,char *d,float f,float p,int gr)     
 //Parameterized constructor
 {

rollno=r;
 fees=f;
 grno=gr;
 per=p;
 int len=strlen(n);
 name=new char[len+1];
 strcpy(name,n);
 len=strlen(d);
 dob=new char[len+1];
 strcpy(dob,d);

}
 void showData()
 {

cout<<"\nGRNO:"<<grno;
 cout<<"\tName:"<<name;
 cout<<"\nRollno:"<<rollno;
 cout<<"\tPer:"<<per;
 cout<<"\nDOB:"<<dob;
 cout<<"\tFees:"<<fees;

}
 };
 //Derived class Account to add bus fees
 class Account:public Student
 {
 protected:
 float busfees;
 float tot;
 public:
 Account():Student()
 {

busfees=0;
 }
 Account(int r,char *n,char *d,float f, float p,int gr,int b):Student(r,n,d,f,p,gr)
 {
 Student();
 busfees=b;
 }
 void totfees()
 {
 tot=busfees+fees;
 }

void showData()
 {
 Student::showData();
 cout<<"\nBus Fees:"<<busfees;
 cout<<"\tTotal Fees:"<<tot<<endl;
 }

};

//Derived class Exam to display result based on percentage

class Exam:public Student
 {
 protected:
 int result;
 public:
 Exam():Student()
 {
 result=0;
 }
 Exam(int r,char *n,char *d,float f, float p,int gr):Student(r,n,d,f,p,gr)
 {

}
 void showResult()
 {

if(per>100)
 cout<<"Invalid result");
 else if(p>=66 && p<=100)
 cout<<"\nYou are in distinction";
 else if(p>=60 && p<66)
 cout<<"\nYou are in First class";
 if(per>=55 && per<60)
 cout<<"\nYou are in higher second class";
 else if(per>=50 && per<55)
 cout<<"\nYou are in Second class";
 else if (per >=40 && per<50)
 cout<<"\nyou are in pass class";
 else if(p<40)
 cout<<"Sorry ! You are fail";

}
 };

// create object of Account class and Exam class
 int main()
 {

Account a(198,"abc","1/2/3",60000,65.5,1234,20000);
 a.totfees();
 a.showData();
 Exam e(210,"xyz","4/5/6",60000,56.5,1234);
 e.showData();
 e.showResult();
 return 0;

}

*******************************PROGRAM 8  ******************************

#include<iostream>
 using namespace std;
 class complex
 {
 int a,b;
 public:
 void getvalue()
 {
 cout<<"Enter the value of Complex Numbers a,b:";
 cin>>a>>b;
 }
 complex operator+(complex ob)
 {
 complex t;
 t.a=a+ob.a;
 t.b=b+ob.b;
 return(t);
 }
 complex operator-(complex ob)
 {
 complex t;
 t.a=a-ob.a;
 t.b=b-ob.b;
 return(t);
 }
 void display()
 {
 cout<<a<<"+"<<b<<"i"<<"\n";
 }
 };

*******************************PROGRAM 9  ******************************

//Demonstration of Hybrid Inheritance using virtual base class
 #include <iostream>
 using namespace std;
 class Student
 {
 private :

string name,dob;
 float fees;
 int grno;

public:
 Student()
 {

name="";
 dob="";
 fees=0;
 grno=0;

}
 Student(string n,string d,float f,int gr)
 {

fees=f;
 grno=gr;
 name=n;
 dob=d;

}
 void showData()
 {

cout<<"\nGRNO:"<<grno;
 cout<<"\tName:"<<name;
 cout<<"\nDOB:"<<dob;
 cout<<"\tFees:"<<fees;

}
 };

//Inheriting Base class Student as virtual base class

class InSem: virtual public Student
 {
 private :
 int marks;
 public:
 InSem():Student()
 {
 marks=0;
 }
 InSem(int m,string n,string d,float f, int gr):Student(n,d,f,gr)
 {
 marks=m;
 }
 int getMarks()
 {
 return marks;
 }
 void showMarks()
 {
 cout<<"\nInSem marks are "<<marks;
 }
 };




//Inheriting Base class Student as virtual base class

class EndSem : public virtual Student
 {
 private :
 int marks;
 public:
 EndSem():Student()
 {
 marks=0;
 }
 EndSem(int m,string n,string d,float f, int gr):Student(n,d,f,gr)
 {
 marks=m;
 }
 int getMarks()
 {
 return marks;
 }
 void showMarks()
 {
 cout<<"\nEndSem marks are "<<marks;
 }
 };

//Hybrid Inheritance of Result class from InSem and EndSem class
 class Result:public InSem,public EndSem
 {
 private:
 int rollno, total;
 float per;
 public:
 Result():InSem(),EndSem()
 {
 rollno=0;
 }
 Result(int r, int m1, int m2,string n, string d,float f,int gr):
InSem(m1,n,d,f,gr),EndSem(m2,n,d,f,gr),Student(n,d,f,gr)
 {
 rollno=r;
 }
 void showResult()
 {

int m1=InSem::getMarks();
 int m2= EndSem::getMarks();
 total=m1+m2;
 InSem::showMarks();
 EndSem::showMarks();
 cout<<"\nTotal marks of Roll no "<<rollno<<" is "<<total;
 if(m1>=20 && m2>=20)
 cout<<"\nYour are pass";
 else
 cout<<"\nYou are fail";

}
 };
 int main()
 {

Result r(123,35,43,"First","11/11/11",60000,12345);
 /* Follwing command creates ambiguity during compilation as two copies are

inherited from Student class. Ambiguity is avoided using virtual keyword */
 r.showData();
 r.showResult();
 return 0;

}

******************************PROGRAM 10 ******************************

//overriding demo using multilevel inheritance

#include<iostream>
 using namespace std;
 class A
 {
 private :
 int a;
 public:
 A()
 {
 a=0;
 }
 A(int a)
 {
 this->a=a;
 }
 void print()
 {
 cout<<"\nA="<<a;

}
 };
 class B:public A
 {
 private:
 int b;
 public:
 B()
 {
 b=0;
 }
 B(int b)
 {
 this->b=b;
 }
 void print()
 {
 cout<<"\nB="<<b;
 }
 };
 class C: public B
 {

int c;
 public:
 C()
 {
 c=0;
 }
 C(int c)
 {
 this->c=c;
 }
 void print()
 {

cout<<"\nC="<<c;
 }
 };

int main()
 {
 C c1;
 c1.A::print();
 c1.B::print();
 c1.print();

return 0;
 }

*******************************PROGRAM 11  *****************************

//Demonstration of Multiple Inheritance
 #include <iostream>
 using namespace std;
 class InSem
 {
 private :
 int marks;
 public:
 InSem()
 {
 marks=0;
 }
 InSem(int m)
 {
 marks=m;
 }
 int getMarks()
 {
 return marks;
 }
 void showMarks()
 {
 cout<<"InSem marks are "<<marks;
 }
 };
 class EndSem
 {
 private :
 int marks;
 public:
 EndSem()
 {
 marks=0;
 }
 EndSem(int m)
 {
 marks=m;
 }
 int getMarks()
 {
 return marks;
 }
 void showMarks()
 {
 cout<<"EndSem marks are "<<marks;
 }
 };
 class Result:public InSem,public EndSem
 {
 private:
 int rollno, total;
 float per;
 public:
 Result():InSem(),EndSem()
 {
 rollno=0;
 }
 Result(int r, int m1, int m2):InSem(m1),EndSem(m2)
 {
 rollno=r;
 }
 void showResult()
 {
 int m1=InSem::getMarks();
 int m2= EndSem::getMarks();
 total=m1+m2;
 cout<<"\nTotal marks of Roll no "<<rollno<<" is "<<total;
 if(m1>=20 && m2>=20)
 cout<<"\nYour are pass";
 else
 cout<<"\nYou are fail";
 }
 };
 int main()
 {
 Result r(111,25,23);
 r.showResult();
 Result r1(222,10,32);
 r1.showResult();
 Result r2(333,47,13);
 r2.showResult();
 return 0;
 }

*******************************PROGRAM 12******************************

//Demonstration of overloading << and >> operator using friend function

#include<iostream>
 using namespace std;
 class Base
 {
 private :
 int A=10;
 int B=10;

public :
 void printA()
 {
 cout<<"\nA="<<A;
 }
 void printB()
 {
 printA();
 cout<<"\nB="<<B;
 }

};

class Derived:public Base
 {
 int C=20;
 public:
 //overloaded insertion (<<) operator
 friend ostream &operator<<(ostream &out, Derived &d)
 {
 out << d.C;
 return out;
 }

//overloaded extraction (>>) opertor

friend istream &operator>>(istream &in, Derived &d)
 {
 in>> d.C;
 return in;
 }
 void printC()
 {
 cout<<"\nC="<<C;
 }
 };
 int main()
 {
 Derived d;
 d.printC();
 d.printB();
 Derived d1;
 cout<<"\nEnter value of object d";
 cin>>d; //overloaded >> operator called in main function
 cout <<"\nvalue of object d is : "<<d;
 //overloaded << operator called in main function

return 0;
 }

*******************************PROGRAM 13 ******************************

//Demonstration of Single inheritance where Base is parent and Derived is child
 #include<iostream>
 using namespace std;
 class Base
 {
 private :
 int A=10;
 void printA()
 {
 cout<<"\nA="<<A;
 }

public :
 int B=10;
 void printB()
 {
 cout<<"\nB="<<B;
 }

};

class Derived:Base
 {
 int C=20;
 public:

void printC()
 {
 cout<<"\nC="<<C;
 }
 };
 int main()
 {
 Derived d;
 d.printC();
 d.printA(); //this line will generate an error as printA() is private
 d.printB();
 return 0;
 }

*******************************PROGRAM 14******************************

//Program of Type conversion : Base to Class

#include<iostream>
 #include<stdlib.h>
 using namespace std;
 class BasicToClass
 {
 private:
 int hr,min,sec;
 public:
 BasicToClass()
 {
 hr=0; min=0;sec=0;
 }
 BasicToClass(int t)
 {
 sec=t%60;
 min=t/60;
 hr=min/60;
 min=min%60;

}
 BasicToClass(float t)
 {
 sec=(int)t%60;
 min=(int)t/60;
 hr=min/60;
 min=min%60;

}

void show()
 {
 cout<<hr<<":"<<min<<":"<<sec;
 }
 };
 int main()
 {
 BasicToClass b;
 b=8900;
 b.show();
 b=3456.45f;
 b.show();
 int i;
 cout<<"Enter time in seconds";
 cin>>i;
 b=i;
 b.show();
 return 0;
 }

*******************************PROGRAM 15 ******************************

//Program of Type conversion : Class to Basic

#include<iostream>
 #include<stdlib.h>
 using namespace std;
 class ClassToBasic
 {
 private:
 int hr,min,sec;
 public:
 ClassToBasic(int h,int m,int s)
 {
 hr=h; min=m; sec=s;
 }
 operator int()
 {
 return hr+min+sec;
 }
 };
 int main()
 {
 ClassToBasic b(2,2,23);
 int i=int(b);
 cout<<i;
 return 0;
 }

*******************************PROGRAM 16******************************

//Program of Type conversion : Class to Class

# include <iostream>
 using namespace std;
 class in1
 {
 int code,items;
 float price;
 public:
 in1(int a,int b,int c)
 {
 code=a;
 items=b;
 price=c;
 }
 void putdata()
 {
 cout<<"CODE= "<<code<<endl;
 cout<<"ITEMS= "<<items<<endl;
 cout<<"VALUE= "<<price<<endl;
 }
 int getcode()
 {
 return code;
 }
 int getitems()
 {
 return items;
 }
 int getprice()
 {
 return price;
 }
 operator float ()
 {
 return items*price;
 }
 };

class in2
 {
 int code;
 float value;
 public:
 in2()
 {
 code=0;
 value=0;
 }
 in2(int x,float y)
 {
 code=x;
 value=y;
 }
 void putdata()
 {
 cout<<"CODE= "<<code<<endl;
 cout<<"VALUE= "<<value<<endl;
 }
 in2(in1 p)
 {
 code=p.getcode();
 value=p.getitems()*p.getprice();
 }
 };

int main()
 {
 in1 s1(100,51,140.0);
 float tot_value;
 in2 d1;
 tot_value=s1;
 d1=in1(s1);
 cout<<"PRODUCT DETAILS INVENT-1 TYPES:->"<<endl;
 s1.putdata();
 cout<<"STOCK VALUE"<<endl;
 cout<<"VALUE= "<<tot_value<<endl;
 cout<<"PRODUCT DETAILS INVENT-2 TYPES:->"<<endl;
 d1.putdata();
 return 0;

}