0% found this document useful (0 votes)
19 views22 pages

Polymorphism Assignment

The document describes polymorphism through inheritance and overriding methods. It defines four base classes - Employee, Shape, PaymentMode - and several derived classes for each. Employee subclasses are SalesManager, Admin, HrManager. Shape subclasses are Rectangle, Circle, Triangle. PaymentMode subclasses are CardPay, Cheque, Upi. Each derived class inherits from and extends the base class, overriding methods like display() and introducing new fields specific to that subclass.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views22 pages

Polymorphism Assignment

The document describes polymorphism through inheritance and overriding methods. It defines four base classes - Employee, Shape, PaymentMode - and several derived classes for each. Employee subclasses are SalesManager, Admin, HrManager. Shape subclasses are Rectangle, Circle, Triangle. PaymentMode subclasses are CardPay, Cheque, Upi. Each derived class inherits from and extends the base class, overriding methods like display() and introducing new fields specific to that subclass.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Polymorphism Assignment

=========================================================================================
1.) Employee (Base Class), SalesManager (Derived Class), Hr ((Derived Class), Admin (Derived Class),
All Derived class inherits from Employee Base Class.

using namespace std;


#include<iostream>
#include<string.h>
struct Employee{
int id;
char name[20];
double sal;

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 setName(const char* nm)


{
strcpy(this->name,nm);
}

void setSalary(double s)
{
this->sal=s;
}

int getId()
{
return this->id;
}

char* getName()
{
return this->name;
}

double getSalary()
{
return this->sal;
}

virtual void display()


{
cout<<"Id:"<<this->id;
cout<<"\nName:"<<this->name;
cout<<"\nSalary:"<<this->sal;
cout<<"\n";
}

//for function overridding, create this function as a virtual


virtual double calSal()
{
return this->sal;
}

}; //Employee class Ends

struct SalesManager : public Employee{


double incentive;
int target;

//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 setIncentive(double inc)


{
this->incentive=inc;
}

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;
}

}; //salesman class ends

//admin class start


struct Admin : public Employee{

double allowance;

Admin() : Employee()
{
cout<<"Admin Default const. call\n";
this->allowance=0;
}

Admin(int i,const char* nm,double s,double a) : Employee(i,nm,s)


{
cout<<"Admin Param. const. call\n";
this->allowance=a;
}

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; //
}

}; //admin class ends

//hrmanager class starts


struct hrManager :public Employee
{

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

void setCommission(double commission)


{
this->commission=commission;
}

//accessors/getters function
double getCommission()
{
return this->commission;
}

void display()
{
Employee :: display();
cout<<"commission: "<<this->commission<<"\n";
cout<<"\n";
}

double calSal()
{
return sal+commission; //
}
};

//display using pointer


int main()
{
Employee* e1;
SalesManager sm1(101,"vikas",30000,12000,3);
e1=&sm1;
e1->display();
cout<<"Total salary of Sales Manager is "<<e1->calSal()<<"\n\n";

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.

using namespace std;


#include<iostream>
struct Shape{
float area;
Shape()
{
cout<<"Shape Default constructor call\n";
this->area=0;
}

Shape(float a)
{
cout<<"\nShape param. constructor call\n";
this->area=a;
}

void setArea(float a)
{
this->area=a;
}

float getArea()
{
return this->area;
}

virtual void display()


{
cout<<"Shape is drawn\n";
}

virtual float calArea()


{
cout<<"area is implemented\n";
return area;
}
};

struct Rectangle : public Shape{


float length,breadth;
Rectangle() : Shape()
{
cout<<"Rectangle Default constructor call\n";
this->length=0;
this->breadth=0;
}

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;
}

}; //Rectangle class ends

struct Circle : public Shape{

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;
}

}; //Circle Class ends here

struct Triangle : public Shape


{
float height,base;
Triangle() : Shape()
{
cout<<"Triangle Default constructor call\n";
this->height=0;
this->base=0;
}

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;
}

}; //Triangle class ends here

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<<"area of circle is: "<<s1->calArea()<<"\n";


cout<<"area= "<<s1->getArea();

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.

using namespace std;


#include<iostream>
#include<string.h>
struct PaymentMode{
char bank_name[20];
char act_no[14];
char ifsc_code[11]; //actually here I know the datatpe is char[] but currently I used int

PaymentMode()
{
cout<<"Payment mode default constructor call\n";
strcpy(this->bank_name,"ng");
strcpy(this->act_no,"ng");
strcpy(this->ifsc_code,"ng");

PaymentMode(const char* nm,const char* ac,const char* ifsc)


{
cout<<"\nPayment mode param. constructor call\n";
strcpy(this->bank_name,nm);
strcpy(this->act_no,ac);
strcpy(this->ifsc_code,ifsc);
}

void setBankName(char* nm)


{
strcpy(this->bank_name,nm);
}

void setActNo(char* ac)


{
strcpy(this->act_no,ac);
}

void setIfsc(char* ifsc)


{
strcpy(this->ifsc_code,ifsc);
}

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";
}

virtual void moneyTransfer()


{
cout<<"we have a different modes for money transfer\n";
}

}; //PaymentMode class ends here

struct CardPay : public PaymentMode


{
int count_swipe;
char pwd[20];

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 setCountSwipe(int csw)


{
this->count_swipe=csw;
}

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

struct Cheque :public PaymentMode{


int chequeNo;

Cheque() : PaymentMode()
{
cout<<"Cheque default constructor call\n";
this->chequeNo=0;
}

Cheque(char* nm,char* ac,char* ifsc,int cno) : PaymentMode(nm,ac,ifsc)


{
cout<<"Cheque param. constructor call\n";
this->chequeNo=cno;
}

void setChequeNo(int cno)


{
this->chequeNo=cno;
}

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";
}

}; //cheque class ends here

struct Upi : public PaymentMode{


char upiid[20];
Upi()
{
cout<<"upi default constructor call\n";
strcpy(this->upiid,"ng");
}
Upi(char* nm,char* ac,char* ifsc,char* id) : PaymentMode(nm,ac,ifsc)
{
cout<<"upi param. constructor call\n";
strcpy(this->upiid,id);
}

void setUpiId(char* id)


{
strcpy(this->upiid,id);
}

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";
}

}; //upi class ends here

int main()
{
cout<<"===Payment Mode Base===\n";
PaymentMode* p;
CardPay cp1;
p=&cp1;

cout<<"===CardPay===\n";
p->display();
p->moneyTransfer();

CardPay cp2("Allahabad","123698623633","ALB1236",2,"abc33"); //using param. const.


p=&cp2;
p->display();
p->moneyTransfer();
//--------------------------------------------------------------//

//for cheque derived class

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;

Cheque ch1(bank_name,act_no,ifsc_code,chequeNo); //using param. constructor


p=&ch1;
p->display();
p->moneyTransfer();
//------------------------------------------------------------------------//

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.

using namespace std;


#include<iostream>
#include<string.h>
struct ModeOfEducation{
int no_of_teacher;
int no_of_student;

ModeOfEducation()
{
this->no_of_teacher=0;
this->no_of_student=0;
}

ModeOfEducation(int nt,int ns)


{
this->no_of_teacher=nt;
this->no_of_student=ns;
}

void setNoOfTeacher(int nt)


{
this->no_of_teacher=nt;
}

void setNoOfStudent(int ns)


{
this->no_of_student=ns;
}

int getNoOfTeacher()
{
return this->no_of_teacher;
}

int getNoOfStudent()
{
return this->getNoOfStudent();
}

virtual void display()


{
cout<<"No of teacher "<<this->no_of_teacher;
cout<<"\nNo of student "<<this->no_of_student;
}

virtual void teach()


{
cout<<"For Batter Teaching Education Mode Necessary\n";
}

};

struct OfflineMode:public ModeOfEducation


{
int noOfBoard;
int noOfBench;
OfflineMode():ModeOfEducation()
{
this->noOfBoard=0;
this->noOfBench=0;
}

OfflineMode(int nt,int ns,int nboard,int nbench):ModeOfEducation(nt,ns)


{
this->noOfBoard=nboard;
this->noOfBench=nbench;

void setNoOfBoard(int nboard)


{
this->noOfBoard=nboard;
}

void setNoOfBench(int nbench)


{
this->noOfBench=nbench;
}

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";
}

};

struct OnlineMode:public ModeOfEducation


{
char nameofApp[20];
int noOfLaptop;
OnlineMode():ModeOfEducation()
{
strcpy(this->nameofApp,"not given");
this->noOfLaptop=0;
}

OnlineMode(int nt,int ns,char* napp,int nl):ModeOfEducation(nt,ns)


{
strcpy(this->nameofApp,napp);
this->noOfLaptop=nl;

void setNameOfApp(char* napp)


{
strcpy(this->nameofApp,napp);
}

void setNoOfLaptop(int nl)


{
this->noOfLaptop=nl;
}

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.

using namespace std;


#include<iostream>
struct Vehicle
{
int regNo;
Vehicle()
{
this->regNo=0;
}

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";
}
};

struct Bike: public Vehicle


{
int stand;
Bike():Vehicle()
{
this->stand=0;
}

Bike(int r,int s):Vehicle(r)


{
this->stand=s;
}

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";
}

};

struct Car : public Vehicle


{
int window;
Car():Vehicle()
{
this->window=0;
}

Car(int r,int w):Vehicle(r)


{
this->window=w;
}

void setWindow(int w)
{
this->window=w;
}

int getWindow()
{
return this->window;
}

//@override function
void brake()
{
cout<<"Drum Brake is applied by car\n";
}

};

struct Bus : public Vehicle


{
int no_of_conductor;
Bus():Vehicle()
{
this->no_of_conductor=0;
}

Bus (int r,int nc):Vehicle(r)


{
this->no_of_conductor=nc;
}

void setNoOfConductor(int nc)


{
this->no_of_conductor=nc;
}

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;
}

You might also like