Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 24

Program for Inheritence with c++

#include<iostream> using namespace std; #include<conio.h> #include<string.h> class student { protected: char* name; int roll; char* university; public: student() { cout<<"*******You are in student default constructor*****"<<endl; name=NULL; roll=0; university=NULL; } student(char n[],int r,char u[]) { cout<<"*******You are in student parametrised constructor*****"<<endl; name=new char[strlen(n)+1]; strcpy(name,n); roll=r; university=new char[strlen(u)+1]; strcpy(university,u); } student(const student& s) { cout<<"*******You are in student copy constructor*****"<<endl; name=new char[strlen(s.name)+1]; strcpy(name,s.name); roll=s.roll; university=new char[strlen(s.university)+1]; strcpy(university,s.university); } ~student() { cout<<"*******You are in student destructor*****"<<endl; delete[] name; delete[] university; } void print_stud() { cout<<"*****STUDENT DETAILS*****"<<endl; cout<<"Name of the sudent:"<<" "<<name<<endl; cout<<"Roll No:"<<" "<<roll<<endl; cout<<"University:"<<" "<<university<<endl; }

}; class pgstudent:public student { protected: char* degree; public: pgstudent() :student() { cout<<"*******You are in pgstudent defalut constructor*****"<<endl; degree=NULL; } pgstudent(char n[],int r,char u[],char d[]) :student(n,r,u) { cout<<"*******You are in pgstudent parametrised constructor*****"<<endl; degree=new char[strlen(d)+1]; strcpy(degree,d); } pgstudent(const pgstudent& p) :student(p) { cout<<"*******You are in copy constructor*****"<<endl; degree=new char[strlen(p.degree)+1]; strcpy(degree,p.degree); } ~pgstudent() { cout<<"*******You are in pgstudent destructor*****"<<endl; delete[] degree; } void print_pg() { student::print_stud(); cout<<"Degree:"<<" "<<degree<<endl; } }; class phd_student:public pgstudent { protected: char* spec; public: phd_student() :pgstudent() { cout<<"*******You are in phd_student defalut constructor*****"<<endl; spec=NULL; } phd_student(char n[],int r,char u[],char d[],char s[]) :pgstudent(n,r,u,d) { cout<<"*******You are in phd_student parametrised constructor*****"<<endl;

spec=new char[strlen(s)+1]; strcpy(spec,s); } phd_student(const phd_student& p) :pgstudent(p) { cout<<"*******You are in phd_student copy constructor*****"<<endl; spec=new char[strlen(p.spec)+1]; strcpy(spec,p.spec); } ~phd_student() { cout<<"*******You are in phd_student destructor*****"<<endl; delete[] spec; } void print_phd() { student::print_stud(); cout<<"Specialisation:"<<" "<<spec<<endl; } }; void main() { phd_student p1("gaurav",1,"IP University","MCA","Research in IT"); p1.print_phd(); phd_student p2=p1; p2.print_phd(); p2.~phd_student(); getch(); }

OUTPUT

LAB EXERCISE 1
#include<iostream> using namespace std; #include<conio.h> #include<typeinfo.h> #define getObjName(str,a,b) #str#a#b #define getMainName(c,s,a,b) a##c##b##s #define decode(s,t,u,m,p,e,d) m##s##u##t #define start getMainName(a,n,m,i) class object { protected: static int count; public: static void increment() { count++; } static void print_inc() { cout<<"No.of times that Object called:"<<count<<endl; } ~object() { count--; } }; int object::count=0; void start() { object obj; object a,b; for(int i=0;i<10;i++) { obj.increment(); } obj.print_inc(); cout<<"******After calling destructor****"<<endl; for(int i=0;i<5;i++) { obj.~object(); } obj.print_inc(); cout<<"Name of the class:"<<" "<<typeid(object).name()<<endl; cout<<"Name of the object:"<<" "<<getObjName(obj)<<endl; cout<<"Name of the object:"<<" "<<getObjName(a)<<endl; cout<<"Name of the object:"<<" "<<getObjName(b)<<endl; getch(); }

OUTPUT

LAB EXERCISE 4
#include<iostream> using namespace std; #include<conio.h> #include<string.h> class employee { protected: char* name; int emp_id; char* dob; char* dep; float bsal; public: employee(); employee(char n[],int a, char d[],char b[],float x); void print_emp(); }; employee::employee() { name=NULL; emp_id=0; dob=NULL; dep=NULL; bsal=0.0; } employee::employee(char n[], int a, char d[], char b[], float x) { name=new char [strlen(n)+1]; strcpy(name,n); emp_id= a; dob=new char[strlen(d)+1]; strcpy(dob,d); dep=new char[strlen(b)+1]; strcpy(dep,b); bsal= x; } void employee::print_emp() { cout<<"****PRINT OF EMPLOYEE****"<<endl; cout<<"name of the employee:"<<name<<endl; cout<<"employee id:"<<emp_id<<endl; cout<<"date of birth:"<<dob<<endl; cout<<"department:"<<dep<<endl; cout<<"basic salary:"<<bsal<<endl; } class regular:public employee { float hra; float da; float cca; float tsal; public:

regular() :employee() { hra=da=cca=tsal=0.0; } regular(char n[], int a, char d[], char b[], float x) :employee(n,a,d,b,x) { hra=x*15/100;; da=x*8/100; cca=x*10/100; tsal=bsal+hra+da+cca; } void print_reg() { employee::print_emp(); cout<<"Total salary of regular employee:"<<tsal<<endl; } }; class adhoc:public employee { float da; float tsal; public: adhoc() :employee() { da=tsal=0.0; } adhoc(char n[], int a, char d[], char b[], float x) :employee(n,a,d,b,x) { da=x*12/100; tsal=bsal+da; } void print_adhoc() { employee::print_emp(); cout<<"Total salary of a adhoc employee:"<<tsal<<endl; } }; class temporary:public employee { float tsal; public: temporary() :employee() { tsal=0.0; } temporary(char n[], int a, char d[], char b[], float x) :employee(n,a,d,b,x) { tsal=bsal; } void print_temp() {

employee::print_emp(); cout<<"Total salary of a temporary employee:"<<tsal<<endl; } }; void main() { regular r("Gaurav",420,"12-oct-2004","marketing",18000.00); r.print_reg(); adhoc ad("Manish",123,"13-jan-2008","purchase",12000.00); ad.print_adhoc(); temporary t2("Prakash",23098,"19-dec-2011","sales",13500.00); t2.print_temp(); getch(); }

OUTPUT

/* Program of swapping using 3 methods */


#include<iostream> using namespace std; #include<conio.h> void swap1(int,int); void swap2(int&,int&); void swap3(int*,int*); void main() { int a,b; a=6; b=3; cout<<"original values are:"<<endl; cout<<"a:"<<a<<endl; cout<<"b:"<<b<<endl; cout<<"****SWAP 1*******"<<endl; swap1(a,b); cout<<"values after swap are:"<<endl; cout<<"a:"<<a<<endl; cout<<"b:"<<b<<endl; cout<<"****SWAP 2*******"<<endl; swap2(a,b); cout<<"values after swap are:"<<endl; cout<<"a:"<<a<<endl; cout<<"b:"<<b<<endl; cout<<"****SWAP 3 after SWAP 2*******"<<endl; swap3(&a,&b); cout<<"values after swap are:"<<endl; cout<<"a:"<<a<<endl; cout<<"b:"<<b<<endl; getch(); } void swap1(int x, int y) { int temp; temp=x; x=y; y=temp; cout<<"swapped values are:"<<endl; cout<<"a:"<<x<<endl; cout<<"b:"<<y<<endl; } void swap2(int &x, int &y) { int temp; temp=x; x=y; y=temp; cout<<"swapped values are:"<<endl; cout<<"a:"<<x<<endl; cout<<"b:"<<y<<endl; } void swap3(int *x, int *y) {

int temp; temp=*x; *x=*y; *y=temp; cout<<"swapped values are:"<<endl; cout<<"a:"<<*x<<endl; cout<<"b:"<<*y<<endl; }

OUTPUT

/*PROGRAM OF PURE VIRTUAL FUNTION*/


#include<iostream> using namespace std; #include<conio.h> class shape { public: virtual float area()=0; virtual float perimeter()=0; virtual void print()=0; }; class square:public shape { protected: float side; public: square(float a) { side=a; } float area() { return(side*side); } float perimeter() { return(4*side); } void print() { cout<<"***SQUARE***"<<endl; cout<<"side of a square:"<<side<<endl; cout<<"area of square:"<<area()<<endl; cout<<"perimeter of square:"<<perimeter()<<endl; } }; class circle:public shape { protected: float radius; public: circle(float b) { radius=b; } float area() { return(3.14*radius*radius); } float perimeter()//perimeter means circumferen of circle { return(2*3.14*radius); }

void print() { cout<<"***CIRCLE***"<<endl; cout<<"radius of circle:"<<radius<<endl; cout<<"area of circle:"<<area()<<endl; cout<<"perimeter of circle:"<<perimeter()<<endl; } }; class cube:public square { protected: float height; public: cube(float a) :square(a) { height=side=a; } float area() { return(6*side*height); } float perimeter() { return(12*height); } void print() { square::print(); cout<<"height of cube:"<<height<<endl; cout<<"area of cube:"<<area()<<endl; cout<<"perimeter of cube:"<<perimeter()<<endl; } }; class cylinder:public circle { protected: float height; public: cylinder(float b,float c) :circle(b) { height=c; } float area() { return(2*3.14*radius*height); } float volume() { return(3.14*radius*radius*height); } void print() { circle::print(); cout<<"height of cylinder:"<<height<<endl;

cout<<"surface area of cylinder:"<<area()<<endl; cout<<"volume of cylinder:"<<volume()<<endl; } }; void main() { cube a1(4.2); a1.print(); cylinder a2(5.2,3.5); a2.print(); getch(); }

OUTPUT

LAB EXERCISE 5
#include<iostream> using namespace std; #include<conio.h> class circle { protected: float radius; public: circle() { cout<<"******Default constructor of circle******"<<endl;; } circle(float a) { cout<<"*****parametrised constructor of circle******"<<endl; radius=a; } float carea() { return(3.14*radius*radius); } void cprint() { cout<<"radius of circle:"<<radius<<endl; cout<<"area of circle:"<<carea()<<endl; } }; class rectangle { protected: float length; float breadth; public: rectangle() { cout<<"****Defualt constructor of rectangle****"<<endl;; } rectangle(float x,float y) { cout<<"******parametrised constructor of rectanle****"<<endl;; length=x; breadth=y; } float rarea() { return(length*breadth); } void rprint() { cout<<"length of rectangle:"<<length<<endl; cout<<"breadth of rectangle:"<<breadth<<endl;

cout<<"area of rectangle:"<<rarea()<<endl; } }; class round_rect:public circle,public rectangle { public: round_rect() :circle(),rectangle() { cout<<"******Defualt constructor of round rect****"<<endl;; } round_rect(float a,float x,float y) :circle(a),rectangle(x,y) { cout<<"*******parametrised constructor of round rect*******"<<endl; } float rr_area() { return((length*breadth)-4*((radius*radius)(3.14*radius*radius)/4)); } void rr_print() { cout<<"radius of circle:"<<radius<<endl; cout<<"length of rectangle:"<<length<<endl; cout<<"breadth of rectangle:"<<breadth<<endl<<endl; cout<<"area of circle:"<<carea()<<endl; cout<<"area of rectangle:"<<rarea()<<endl; cout<<"area of round rect:"<<rr_area()<<endl; } }; void main() { round_rect r2; round_rect r1(2,3,3); r1.rr_print(); getch(); }

OUTPUT

/*PROGRAM OF VIRTUAL FUNTION */


#include<iostream> using namespace std; #include<string.h> #include<conio.h> class book { protected: char* bname; int bid; char* author; float price; public: book() { cout<<"****Default constructor of book class****"<<endl; bname=NULL; bid=0; author=NULL; price=0.00; } book(char bn[],int a,char au[],float p) { cout<<"****parametrised constructor of book class*****"<<endl; bname=new char[strlen(bn)+1]; strcpy(bname,bn); bid=a; author=new char[strlen(au)+1]; strcpy(author,au); price=p; } virtual void print() { cout<<"Name of the book:"<<bname<<endl; cout<<"Book id:"<<bid<<endl; cout<<"Name of author:"<<author<<endl; cout<<"Price of book:"<<price<<endl; } }; class course_book:public book { protected: char* subject; public: course_book() :book() { cout<<"******Default constructor of course class******"<<endl; subject=NULL; } course_book(char nm[],int x,char th[],float y,char s[]) :book(nm,x,th,y) {

cout<<"*******parametrised constructor of course class******"<<endl; subject=new char[strlen(s)+1]; strcpy(subject,s); } void print() { book::print(); cout<<"Name of subject:"<<subject<<endl; } }; class issuable_book:public course_book { protected: char* DOI; char* DOR; const char status; public: issuable_book() :course_book(),status('\0') { cout<<"******Default constructor of issuable class*****"<<endl; DOI=NULL; DOR=NULL; } issuable_book(char bn[],int a,char au[],float p,char s[],char d[],char r[],char s1) :course_book(bn,a,au,p,s),status(s1) { cout<<"******parametrised constructor of issuable class*******"<<endl; DOI=new char[strlen(d)+1]; strcpy(DOI,d); DOR=new char[strlen(r)+1]; strcpy(DOR,r); } void print() { course_book::print(); cout<<"Date of issue:"<<DOI<<endl; cout<<"Date of return:"<<DOR<<endl; cout<<"status:"<<status<<endl<<endl; } }; class reference_book:public course_book { protected: const char status; public: reference_book() :course_book(),status('\0') { cout<<"********Default constructor of reference class******"<<endl; } reference_book(char bn[],int a,char au[],float p,char s[],char R) :course_book(bn,a,au,p,s),status(R)

{ cout<<"*******parametrised constructor of reference class*****"<<endl; } void print () { course_book::print(); cout<<"Reference status:"<<status<<endl; } }; void main() { issuable_book a1("mastering in c++",1234,"venugopal",320.50,"oop in c++","16-mar-2012","28-mar-2012",'A'); a1.print(); reference_book a2("oop with c++",4502,"E Balagurusamy",452.00,"oop in c++",'Y'); a2.print(); getch(); }

OUTPUT

You might also like