You are on page 1of 20

PROGRAM-27 W.A.P TO OVERLOAD UNARY INCREMENT(++) OPERATOR.

#include<iostream.h> #include<conio.h> class counter { int count; public: counter():count(0) {} int get_count() { return count; } void operator ++ () { count++; } }; void main() { clrscr(); counter c1,c2; cout<<"\n c1= "<<c1.get_count(); cout<<"\n c2= "<<c2.get_count(); ++c1; ++c2; ++c2; ++c1; ++c2; ++c2; ++c1; cout<<"\n c1= "<<c1.get_count(); cout<<"\n c2= "<<c2.get_count(); getch(); }

PROGRAM-28
W.A.P TO OVERLOAD BINARY + OPERATOR.
#include<iostream.h> #include<conio.h> class complex { float x,y; public: complex() {x= 0.0;y=0.0; } void get() { cout<<"\nEnter real part "; cin>>x; cout<<"Enter imaginary part "; cin>>y; } void show() { cout<<x<<" + i"<<y; } complex operator+(complex c) { complex temp; temp.x=x+c.x; temp.y=y+c.y; return temp; }}; void main() { clrscr(); complex c1,c2,c3; cout<<"Enter a complex no. "; c1.get(); cout<<"\nEnter another complex no. "; c2.get(); c3=c1+c2; cout<<"\nSum of both is: "; c3.show(); getch(); }

PROGRAM-29
W.A.P TO OVERLOAD BINARY < OPERATOR.
#include<iostream.h> #include<conio.h> class time { int hrs,min,sec; public: void input(); void show(); int operator<(time); }; void time::input() {cout<<"\nEnter Hours "; cin>>hrs; cout<<"Enter Minutes "; cin>>min; cout<<"Enter Seconds "; cin>>sec; do{ if(sec>=60) {min=min+1; sec=sec-60; } if(min>=60) { hrs=hrs+1; min=min-60; }}while((min>60)||(sec>60)); } void time::show() { cout<<"\nhrs:min:sec "<<hrs<<":"<<min<<":"<<sec; } int time::operator<(time t) { long int tsec,tsec1; tsec=sec+(min*60)+(hrs*60*60); tsec1=t.sec+(t.min*60)+(t.hrs*60*60); if(tsec<tsec1) return 1; else return 0; } void main() { clrscr();

time t1,t2; cout<<"Enter Time 1 : "; t1.input(); cout<<"\nEnter Time 2 : "; t2.input(); cout<<"\nTime 1 : "; t1.show(); cout<<"\n\nTime 2 : "; t2.show(); if(t1<t2) cout<<"\n\nTime 1 is smaller than Time 2 "; else cout<<"\n\nTime 1 is not smaller than Time 2 "; getch(); }

PROGRAM-30 W.A.P TO OVERLOAD ASSIGNMENT (=) OPERATOR.


#include<iostream.h> #include<conio.h> class complex { float x,y; public: complex() {x= 0.0;y=0.0; } void get() { cout<<"\nEnter real part "; cin>>x; cout<<"Enter imaginary part "; cin>>y; } void show() { cout<<x<<" + i"<<y; } complex operator=(complex c) { x=c.x; y=c.y; return c; }}; void main() { clrscr(); complex c1,c2; cout<<"Enter a complex no. "; c1.get(); cout<<"\nEnter another complex no. "; c2.get(); cout<<"\nc1= "; c1.show(); cout<<"\nc2= "; c2.show(); c1=c2; cout<<"\n\nAfter appling assignment operator "; cout<<"\n\nc1= "; c1.show(); cout<<"\nc2= "; c2.show(); getch(); }

PROGRAM-32 W.A.P TO OVERLOAD UNARY MINUS(-)OPERATOR USING FRIEND FUNCTION.


#include<iostream.h> #include<conio.h> class space { int x,y,z; public: void getdata(); void display(); friend void operator-(space &s); }; void space::getdata() { cout<<"Enter three numbers "; cin>>x>>y>>z; } void space::display() { cout<<"\n\n Three Numbers are:"; cout<<x<<" "; cout<<y<<" "; cout<<z<<" "; } void operator-(space &s) { s.x= -s.x; s.y= -s.y; s.z= -s.z; } void main() { clrscr(); space s1; s1.getdata(); s1.display(); -s1; cout<<"\n\n After Negation we have: "; s1.display(); getch(); }

PROGRAM-34 Create a base class basic_info with data members name,roll no,sex and two member fuctions getdata and display.derive a class physical fit from basic_info which has the data members height and weight and member functions getdata and display. Display all the information using object of derived class.
#include<iostream.h> #include<conio.h> #include<stdio.h> class basic_info { char name[20],sex; long roll; public: void getdata(); void display(); }; void basic_info::getdata() { cout<<"\n Enter your name:"; gets(name); cout<<"\n Enter your Sex:"; cin>>sex; cout<<"\n Enter your Roll number: "; cin>>roll; } void basic_info :: display() { cout<<"\n Name:"; puts(name); cout<<"\n Sex:";cout<<sex; cout<<"\n\n Roll Number:";cout<<roll; } class physical_fit:public basic_info { float height,weight; public: void getdata(); void display(); }; void physical_fit::getdata() { basic_info::getdata(); cout<<"\n Enter your height(cms): "; cin>>height; cout<<"\n Enter your weight(kgs): "; cin>>weight; } void physical_fit::display()

{ basic_info::display(); cout<<"\n\n Height(cms):"<<height; cout<<"\n\n Weight(kgs):"<<weight; } void main() { basic_info b; physical_fit p; clrscr(); p.getdata(); clrscr(); p.display(); getch(); }

PROGRAM-35 Create class first with data members book no,book name and member function getdata and putdata.create a class second with data members author name, publisher and member function getdata and showdata .Derive a class third from first and second with data members no of pages and year of publication. Display all these information using array of objects of third class.
#include<iostream.h> #include<conio.h> #include<stdio.h> class first { char bk_no[20]; char bk_name[50]; public: void getdata1() { cout<<"\n Enter Book Name: "; gets(bk_name); cout<<"\n Enter Book Number: "; gets(bk_no); } void putdata1() { cout<<"\n Book Number:"; puts(bk_no); cout<<"\n Book Name:"; puts(bk_name); } }; class second { char athr_name[20],publisher[80]; public: void getdata2() { cout<<"\n Enter Author Name: "; gets(athr_name); cout<<"\n Enter Publisher: "; gets(publisher); } void putdata2() { cout<<"\n Author Number:";

puts(athr_name); cout<<"\n Publisher:"; puts(publisher); } }; class third:public first,public second { long pages,year; public: void getdata3() { getdata1(); getdata2(); cout<<"\n Enter Number of Pages: "; cin>>pages; cout<<"\n Enter year of Publication: "; cin>>year; } void putdata3() { putdata1(); putdata2(); cout<<"\n Number of Pages are: "<<pages; cout<<"\n Year of Publication is: "<<year; } }; void main() { third t[50]; int n; clrscr(); cout<<"\n How many book's information you want to enter:"; cin>>n; for(int i=1;i<=n;i++) { t[i].getdata3(); clrscr(); } for(i=1;i<=n;i++) { clrscr(); t[i].putdata3(); cout<<"\n\n\n Press Enter to continue..........\n"; getch(); } }

PROGRAM-37 Design three classes student,exam,result.the student class has datamembers such as roll no,name.Create a class exam by inheriting the student class.The exam class adds data members representing the marks scored in six subjects.Derive the result from exam class and has it own data members such as totalmarks.Write a program to model this relationship.
#include<iostream.h> #include<conio.h> #include<stdio.h> class student { public: long int roll,cls; char name[40]; void getdata() { cout<<"\n Enter your name:"; gets(name); cout<<"\n Enter your class:";cin>>cls; cout<<"\n Enter your roll number:";cin>>roll; } void display() { cout<<"\n Name: "; puts(name); cout<<"\n Class: "<<cls; cout<<"\n Roll number: "<<roll; } }; class exam:public student { public: int marks[6]; void getdata1() { getdata(); cout<<"\n Enter your marks in six subjects:"; for(int i=1;i<=6;i++) { cout<<"\n Subject"<<i<<": "; cin>>marks[i]; } } void display1()

{ display(); cout<<"\n Marks in Six subjects:"; for(int i=1;i<=6;i++) { cout<<"\n Subject"<<i<<": "; cout<<marks[i]; } } }; class result:public exam { int totalmarks,avg; public: void getdata() { getdata1(); } void final_result() { display1(); totalmarks=0; for(int i=1;i<=6;i++) { totalmarks=totalmarks+marks[i]; } cout<<"\n Total Marks are: "<<totalmarks; avg=totalmarks/6; cout<<"\n Percentage is: "<<avg<<"%"; } }; void main() { result r; clrscr(); r.getdata(); r.final_result(); getch(); }

You might also like