Polymorphism Assignment
Polymorphism Assignment
=========================================================================================
1.) Employee (Base Class), SalesManager (Derived Class), Hr ((Derived Class), Admin (Derived Class),
All Derived class inherits from Employee Base Class.
Employee()
{
cout<<"Employee Default Constructor call\n";
this->id=0;
strcpy(this->name,"not given");
this->sal=0;
}
Employee(int i,const char* nm,double s)
{
cout<<"Employee Parametrized Constrtuctor call\n";
this->id=i;
strcpy(this->name,nm);
this->sal=s;
}
void setId(int i)
{
this->id=i;
}
void setSalary(double s)
{
this->sal=s;
}
int getId()
{
return this->id;
}
char* getName()
{
return this->name;
}
double getSalary()
{
return this->sal;
}
//default constructor
SalesManager() : Employee()
{
cout<<"Sales Manager Default Const. Call\n";
this->incentive=0;
this->target=0;
}
//parametrized constructor
SalesManager(int id,const char* name,double sal,double inc,int t) : Employee(id,name,sal)
{
cout<<"Sales Manager Parametrized const. call\n";
this->incentive=inc;
this->target=t;
}
void setTarget(int t)
{
this->target=t;
}
//getters method
double getIncentive()
{
return this->incentive;
}
int getTarget()
{
return this->target;
}
void display()
{
Employee::display(); //base class ke value display karane ke liye :: (scope resolution use kiya)
cout<<"Incentive: "<<this->incentive;
cout<<"\nTarget: "<<this->target<<"\n\n";
}
double calSal()
{
// return this->sal+this->incentive;
//or
return sal+incentive;
}
double allowance;
Admin() : Employee()
{
cout<<"Admin Default const. call\n";
this->allowance=0;
}
void setAllowance(double a)
{
this->allowance=a;
}
double getAllowance()
{
return this->allowance;
}
void display()
{
Employee :: display();
cout<<"Allowance: "<<this->allowance<<"\n\n";
double calSal()
{
return sal+allowance; //
}
double commission;
//default constructor
hrManager() : Employee()
{
cout<<"HR Default const. call\n";
this->commission=2000;
}
//param. constructor
hrManager(int id,const char* n,double sal,double comm) : Employee(id,n,sal)
{
cout<<"HR Parametrized const. call\n";
this->commission=comm;
}
//mutators/setters function
//accessors/getters function
double getCommission()
{
return this->commission;
}
void display()
{
Employee :: display();
cout<<"commission: "<<this->commission<<"\n";
cout<<"\n";
}
double calSal()
{
return sal+commission; //
}
};
Admin a1(201,"ankit",20000,6000);
e1=&a1;
e1->display();
cout<<"Total salary of Admin is "<<e1->calSal()<<"\n\n";
hrManager hr1(301,"abhi",18000,2000);
e1=&hr1;
e1->display();
cout<<"Total salary of Hr Manager is "<<e1->calSal()<<"\n\n";
}
=======================================================================================
2.) Shape (Base Class), Rectangle (Derived Class), Circle ((Derived Class), Triangle (Derived Class),
All Derived class inherits from Shape Base Class.
Shape(float a)
{
cout<<"\nShape param. constructor call\n";
this->area=a;
}
void setArea(float a)
{
this->area=a;
}
float getArea()
{
return this->area;
}
Rectangle(float l,float b)
{
cout<<"Rectangle Parametrized constructor call\n";
this->length=l;
this->breadth=b;
}
void setLength(float l)
{
this->length=l;
}
void setBreadth(float b)
{
this->breadth=b;
}
float getLength()
{
return this->length;
}
float getBreadth()
{
return this->breadth;
}
void display()
{
//Shape :: display(); //ye jaruri nhi h es prog. me employee me id,name,sal display karana tha esliye
jaruri the
cout<<"Shape is drawn with length is "<<this->length<<" and breadth is "<<this->breadth<<"\n";
}
float calArea()
{
float ar;
ar=length*breadth;
this->setArea(ar); //ye set esliye kiya because yadi main me getArea() call kiya to woh 0 dega
return ar;
}
float radius;
Circle() : Shape()
{
cout<<"Circle Default constructor call\n";
this->radius=0;
}
Circle(float r)
{
cout<<"Circle param. constructor call\n";
this->radius=r;
}
void setRadius(float r)
{
this->radius=r;
}
float getRadius()
{
return this->radius;
}
void display()
{
Shape :: display();
{
cout<<"Circle is drawn with radius "<<this->radius<<"\n";
}
}
float calArea()
{
float ar;
ar=3.14*this->radius*this->radius;
this->setArea(ar); //ye set esliye kiya because yadi main me getArea() call kiya to woh 0 dega
return ar;
}
Triangle(float h,float b)
{
cout<<"Triangle param. constructor call\n";
this->height=h;
this->base=b;
}
void setHeight(float h)
{
this->height=h;
}
void setBase(float b)
{
this->base=b;
}
float getHeight()
{
return this->height;
}
float getBase()
{
return this->base;
}
void display()
{
Shape :: display();
cout<<"shape is drawn with height "<<this->height<<" & "<<"base "<<this->base<<"\n";
}
float calArea()
{
float ar;
ar=0.5*this->height*this->base;
this->setArea(ar); //ye set esliye kiya because yadi main me getArea() call kiya to woh 0 dega
return ar;
}
int main()
{
cout<<"===Rectangle===\n";
Shape* s1;
Rectangle rt2(12,6);
s1=&rt2;
s1->display();
cout<<"area of rectangle is: "<<s1->calArea();
cout<<"area= "<<s1->getArea(); //calArea() & getArea() dono
cout<<"\n===Circle===\n";
Circle ci1;
float r;
cout<<"Enter the radius\n";
cin>>r;
ci1.setRadius(r);
s1=&ci1;
cout<<"\n===Triangle===\n";
float h,b;
Triangle t1;
cout<<"Enter height\n";
cin>>h;
t1.setHeight(h);
cout<<"Enter base\n";
cin>>b;
t1.setBase(b);
s1=&t1;
cout<<"area of Triangle is: "<<s1->calArea()<<"\n";
cout<<"area= "<<s1->getArea();
return 0;
}
3.) PaymentMode (Base Class), CardPay (Derived Class), Cheque ((Derived Class), Upi (Derived Class),
All Derived class inherits from PaymentMode Base Class.
PaymentMode()
{
cout<<"Payment mode default constructor call\n";
strcpy(this->bank_name,"ng");
strcpy(this->act_no,"ng");
strcpy(this->ifsc_code,"ng");
char* getBankName()
{
return this->bank_name;
}
char* getActNo()
{
return this->act_no;
}
char* getIfsc()
{
return this->ifsc_code;
}
void display()
{
cout<<"Bank Name: "<<this->bank_name<<"\n";
cout<<"Account No: "<<this->act_no<<"\n";
cout<<"Ifsc Code: "<<this->ifsc_code<<"\n";
}
CardPay():PaymentMode()
{
cout<<"Card Pay default constructor call\n";
this->count_swipe=0;
strcpy(this->pwd,"ng");
}
CardPay(const char* nm,const char* ac,const char* ifsc,int csw,const char* p) : PaymentMode(nm,ac,ifsc)
{
cout<<"Card Pay param. constructor call\n";
this->count_swipe=csw;
strcpy(this->pwd,p);
}
void setPassword(char* p)
{
strcpy(this->pwd,p);
}
int getCountSwipe()
{
return this->count_swipe;
}
char* getPassword()
{
return this->pwd;
}
void display()
{
PaymentMode::display();
cout<<"No. of Swipe Count: "<<count_swipe<<"\n";
cout<<"Password: "<<pwd<<"\n";
}
//@overide function
void moneyTransfer()
{
cout<<"Swipe Machine is used for money transfer\n";
}
}; //payment mode class ends here
Cheque() : PaymentMode()
{
cout<<"Cheque default constructor call\n";
this->chequeNo=0;
}
int getChequeNo()
{
return this->chequeNo;
}
void display()
{
PaymentMode :: display();
cout<<"Cheque No: "<<chequeNo<<"\n";
}
//@overide function
void moneyTransfer()
{
cout<<"Signature/cheque book is used for money transfer\n";
}
char* getUpiId()
{
return this->upiid;
}
void display()
{
PaymentMode :: display();
cout<<"UpiId: "<<upiid<<"\n";
}
//@overide function
void moneyTransfer()
{
cout<<"Upi id is used for money transfer\n";
}
int main()
{
cout<<"===Payment Mode Base===\n";
PaymentMode* p;
CardPay cp1;
p=&cp1;
cout<<"===CardPay===\n";
p->display();
p->moneyTransfer();
cout<<"\n===Cheque===\n";
char bank_name[20];
char act_no[14];
char ifsc_code[11];
int chequeNo;
cout<<"\nEnter Bank Name\n";
cin>>bank_name;
cout<<"Enter Account No.\n";
cin>>act_no;
cout<<"Enter Ifsc code\n";
cin>>ifsc_code;
cout<<"Enter cheque No\n";
cin>>chequeNo;
cout<<"\n===UPI===\n";
char upiid[20];
cout<<"\nEnter Bank Name\n";
cin>>bank_name;
cout<<"Enter Account No.\n";
cin>>act_no;
cout<<"Enter Ifsc code\n";
cin>>ifsc_code;
cout<<"Enter upi id\n";
cin>>upiid;
//yahan default const. bhi call hoga pehle base class(payment) ka then derived(upi) ka
u1.setBankName(bank_name);
u1.setActNo(act_no);
u1.setIfsc(ifsc_code);
u1.setUpiId(upiid);
u1.display();
u1.moneyTransfer();
p=&u1;
return 0;
}
4.) ModeOfEducation (Base Class), OfflineMode (Derived Class), OnlineMode ((Derived Class),
All Derived class inherits from ModeOfEducation Base Class.
ModeOfEducation()
{
this->no_of_teacher=0;
this->no_of_student=0;
}
int getNoOfTeacher()
{
return this->no_of_teacher;
}
int getNoOfStudent()
{
return this->getNoOfStudent();
}
};
int getNoOfBoard()
{
return this->noOfBoard;
}
int getNoOfBench()
{
return this->noOfBench;
}
void display()
{
ModeOfEducation::display();
cout<<"\nNo Of Board "<<this->noOfBoard;
cout<<"\nNo Of Bench "<<this->noOfBench;
}
void teach()
{
cout<<"\nTeaching done in Offline mode";
}
};
char* getNameOfApp()
{
return this->nameofApp;
}
int getNoOfLaptop()
{
return this->noOfLaptop;
}
void display()
{
ModeOfEducation::display();
cout<<"\nName of App "<<this->nameofApp;
cout<<"\nNo Of Laptop "<<this->noOfLaptop;
}
void teach()
{
cout<<"\nTeaching done in Virtual mode";
}
};
int main()
{
int nt,ns,nboard,nbench;
ModeOfEducation* m1;
OfflineMode om1;
m1=&om1;
cout<<"Enter No Of Teacher\n";
cin>>nt;
om1.setNoOfTeacher(nt);
cout<<"Enter No of Student\n";
cin>>ns;
om1.setNoOfStudent(ns);
cout<<"Enter No of Board\n";
cin>>nboard;
om1.setNoOfBoard(nboard);
cout<<"Enter No of Bench\n";
cin>>nbench;
om1.setNoOfBench(nbench);
m1->display();
m1->teach();
char napp[20];
int nl;
OnlineMode on1;
m1=&on1;
cout<<"\n\nEnter No Of Teacher\n";
cin>>nt;
on1.setNoOfTeacher(nt);
cout<<"Enter No of Student\n";
cin>>ns;
on1.setNoOfStudent(ns);
cout<<"Enter Name of App\n";
cin>>napp;
on1.setNameOfApp(napp);
cout<<"Enter No of Laptop\n";
cin>>nl;
on1.setNoOfLaptop(nl);
m1->display();
m1->teach();
return 0;
}
5.) Vehicle (Base Class), Bike (Derived Class), Car (Derived Class),
Bus (Derived Class),
All Derived class inherits from Vehicle Base Class.
Vehicle(int r)
{
this->regNo=r;
}
void setRegNo(int r)
{
this->regNo=r;
}
int getReegNo()
{
return this->regNo;
}
//override function
virtual void brake()
{
cout<<"Brake is apply\n";
}
};
void setStand(int s)
{
this->stand=s;
}
int getStand()
{
return this->stand;
}
//@override function
void brake()
{ Vehicle::brake();
cout<<"Disc Brake is applied by bike\n";
}
};
void setWindow(int w)
{
this->window=w;
}
int getWindow()
{
return this->window;
}
//@override function
void brake()
{
cout<<"Drum Brake is applied by car\n";
}
};
int getNoOfConductor()
{
return this->no_of_conductor;
}
//@override function
void brake()
{
cout<<"Air Brake is applied by bus\n";
}
};
int main()
{
Vehicle* v1;
Bike b1;
v1=&b1;
v1->brake();
Car c1;
v1=&c1;
v1->brake();
Bus bu1;
v1=&bu1;
v1->brake();
return 0;
}