You are on page 1of 31

*/ Write a program to create a Class or Object \*

#include<iostream.h> #include<conio.h> #include<stdio.h> class student { private: int rollno; char name[]; char clas[]; float marks; float fees; public: void getdata() { cout<<"enter the roll no of student"; cin>>rollno; cout<<"name"; cin>>name; cout<<"class"; cin>>clas; cout<<"marks"; cin>>marks; cout<<"fees"; cin>>fees; } void putdata() { cout<<"\nroll no"<<rollno; cout<<"\nname"<<name; cout<<"\nclass"<<clas; cout<<"\nmarks"<<marks; cout<<"\nfees"<<fees; } }; void main()

{ student s[5]; int i; for(i=0;i<5;i++) { s[i].getdata(); } for(i=0;i<5;i++) { s[i].putdata(); } getch(); }

*/ Write a program of Array of Object \*

#include<iostream.h> #include<stdio.h> #include<conio.h> class student { private: int rollno; char name[15]; char clas[10]; float marks; float fees; public: char grade; void getdata() { cout<<"\nenter the roll no of student"; cin>>rollno; cout<<"\nname"; cin>>name; cout<<"\nclass"; cin>>clas; cout<<"\nmarks"; cin>>marks; cout<<"\nfees"; cin>>fees; } void putdata() { cout<<"\nroll no"<<rollno; cout<<"\nname"<<name; cout<<"\nclass"<<clas; cout<<"\nmarks"<<marks; cout<<"\nfees"<<fees; } };

void main() { clrscr(); student s1,s2; s1.getdata(); s2.getdata(); s1.putdata(); s2.putdata(); s1.grade='a'; s2.grade='b'; cout<<"\nthe grade of first student is"<<s1.grade; cout<<"\nthe grade of second student is"<<s2.grade; getch(); }

*/ Write a program of Default Constructor \*

#include<stdio.h> #include<iostream.h> #include<conio.h> class consdemo { private: int x,y; public: consdemo() { x=10; y=10; } void input() { cout<<"enter the value of x and y"; cin>>x>>y; } void display() { cout<<"\nthe value of x is"<<x; cout<<"\n the value of y is"<<y; } }; void main() { clrscr(); consdemo obj1,obj2; obj1.display(); obj2.input(); obj2.display(); getch(); }

*/ Write a program of Parameterized Constructor \*

#include<iostream.h> #include<stdio.h> #include<conio.h> class consdemo { private: int x,y,z; public: consdemo() { x=0; y=0; z=0; cout<<"\n the delault constructor"; } consdemo(int a,int b) { x=a; y=b; z=0; cout<<"\n the constructor with 2 parameter"; } consdemo(int a,int b,int c) { x=a; y=b; z=c; cout<<"\n the constructor with 3 parameter"; } void display() { cout<<"\n the value of x is"<<x; cout<<"\n the value of y is"<<y; cout<<"\n the value of z is"<<z;

} }; void main() { clrscr(); consdemo obj1; obj1.display(); consdemo obj2(10,20); obj2.display(); consdemo obj3(10,20,30); obj3.display(); getch(); }

*/ Write a program of Copy a Constructor \*

#include<iostream.h> #include<stdio.h> #include<conio.h> class copydemo { private: int a,b,c; public: copydemo() { a=0; b=0; c=0; } copydemo(int x,int y,int z) { a=x; b=y; c=z; } copydemo (copydemo&obj) { a=obj.a; b=obj.b; c=obj.c; } void display() { cout<<"\n the value of a is"<<a; cout<<"\n the value of b is"<<b; cout<<"\n the value of c is"<<c; } };

void main() { clrscr(); copydemo c1(10,20,30); c1.display(); copydemo c2(c1); c2.display(); getch(); }

*/ Write a program of demonstrate the Object as Function \*

#include<iostream.h> #include<conio.h> #include<stdio.h> class distance { private: int meter; float cm; public: void getdata(); void display(); void adddist(distance,distance); }; void distance::getdata() { cout<<"\n enter meter"; cin>>"meter"; cout<<"\nenter centimeter"; cin>>"cm"; } void distance::display() { cout<<"\nmeter="<<meter; cout<<"\ncentimeter="<<cm; } void distance::adddist(distance dd1,distance dd2) { meter=dd1.meter+dd2.meter; cm=dd1.cm+dd2.cm; if(cm>=100) { meter++; cm-=100;

} } void main() { clrscr(); distance dd1,dd2,dd3; cout<<"enter the distance for first object"; dd1.getdata(); dd1.display(); dd2.getdata(); dd2.display(); dd3.adddist (dd1,dd2); dd3.display(); getch(); }

*/ Write a program of returning of Object from Function \*


#include<iostream.h> #include<conio.h> #include<stdio.h> class distance { private: int meter; float cm; public: void getdata(); void display(); distance adddist(distance); }; void distance::getdata() { cout<<"\n enter meter"; cin>>meter; cout<<"\nenter centimeter"; cin>>cm; } void distance::display() { cout<<"\nmeter="<<meter; cout<<"\ncentimeter="<<cm; } distance distance::adddist(distance dd1) { distance temp; temp.meter=meter+dd1.meter; temp.cm=cm+dd1.cm; if(temp.cm>=100) { temp.meter++; temp.cm-=100; } return temp; }

*/ Write a program of that demonstrate the Unary Operator Overloading \*

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

*/ Write a program of that demonstrate the Binary Operator Overloading \*

#include<iostream.h> #include<conio.h> #include<stdio.h> class distance { private: int feet; float inches; public: distance():feet(0),inches(0.0) {} dintance(int ft,float in):feet(ft),inches(in) { } void get_dist() { cout<<"\nenter feet"<<feet; cout<<"\nenter inches<<"inches; void show dist() { cout<<feet<<" "<<inches distance operator+(distance) }; distance distance::operator+(distance d2) distance temp; temp.inches=inches+d2.inches if(temp.inches>=12.0) { temp.inches-=12.0; temp.feet++; } return temp; } void main() distance dist1,dist3; dist1.getdist() ; distance dist2(11,6.25)

dist3=dist1+dist2; getch(); }

*/ W.A.P of that demonstrate the concept of Public in Inheritance \*

#include<stdio.h> #include<conio.h> #include<iostream.h> class first { protected: int a; protected: int b; public: int c; }; class second:public first { private: int p; protected: int q; public: int r; just() { a=0; b=1; c=2; p=10; q=11; r=2; } }; void main() { second sobj; /*sobj.a=10; sobj.b=10;*/ sobj.c=10; /*sobj.p=10; sobj.q=10;*/ sobj.r=10; getch(); }

*/ W.A.P of that demonstrate the concept of Private in Inheritance \*


#include<stdio.h>

#include<conio.h> #include<iostream.h> class first { private: int a; protected: int b; public: int c; }; class second:protected first { private: int p; protected: int q; public: int r; just() { a=0; b=1; c=2; p=10; q=11; r=2; } void main() { second sobj; sobj.a=10; sobj.b=10; sobj.c=10; sobj.p=10; sobj.q=10; sobj.r=10; getch(); }

*/ W.A.P of that demonstrate the concept of Protected in Inheritance \*


#include<stdio.h> #include<conio.h> #include<iostream.h>

class first { private: int a; protected: int b; public: int c; }; class second:protected first { private: int p; protected: int q; public: int r; just() { a=0; b=1; c=2; p=10; q=11; r=2; } void main() { second sobj; sobj.a=10; sobj.b=10; sobj.c=10; sobj.p=10; sobj.q=10; sobj.r=10; getch(); }

/*Write aprogram of that demonstrate the concept Constructor or Destructor in inheritance \*


#include<stdio.h> #include<conio.h> #include<iostream.h> class father { public: father() { cout<<father constructor; } ~father { cout<<father destructor; } }; class mother { public: mother() { cout<<constructor mother; } ~mother { cout<<mother destructor; } }; class baby:public father ,public mother { public: baby() { cout<<baby constructor; } ~baby() { cout<<baby destructor; } };

void main() { baby b; } getch(); }

/*Write a program that shows the concept of virtual functions*\


#include<stdio.h> #include<conio.h> #include<iostream.h> class base { public: void display() { cout<<" display base"; } virtual void show() { cout<<" show base"; } }; class derived:public base { public: void display() { cout<<" display derived"; } void show() { cout<<" show derived"; } }; void main() { clrscr(); base b; derived d; base *bptr; bptr=&b; bptr->display(); bptr->show(); bptr=&d; bptr->display(); bptr->show(); getch(); }

#include<conio.h> #include<stdio.h> #include<iostream.h> class DF; class DM { private: int meter; int cm; public: void getdata() { cout<<"enter value in meters"; cin>>meter; cout<<"enter values in cms"; cin>>cm; } void putdata() { cout<<"the meters are"<<meter; cout<<"the centimeters are"<<cm; } friend void sum(DM a,DF b) }; class DF { private: int feet; int inches; public: void getdata() { cout<<"enter value in feet"; cin>>feet; cout<<"enter the value in inches"; cin>>inches; } void putdata() { cout<<"the feet are"<<feet; cout<<"the inches are"<<inches; }

friend void sum(DM a,DF b) }; void sum(DM a,DF b) { double temp,f,i; temp=a.meter*39.37+(a.cm/100)*39.37+b.feet*12+b.inches; f=int(temp)/12; i=int(temp)%12; cout<<"the distance after addtion is" <<f<<i; } void main() { clrscr(); DM d1; DF d2; d1.getdata(); d2.getdata(); d1.putdata(); d2.putdata(); sum(d1,d2); getch(); }

#include<conio.h> #include<stdio.h> #include<iostream.h> class shape { protected: double x,y; public: void getdata(double x1,double y1) { x=x1; y=y1; } virtual void displayarea()=0; }; class triangle:public shape { public: void getdata(double x1,double y1) { shape::getdata(x1,y1); } void displayarea() { double area=(x*y)/2; cout<<"area of triangle is"<< area; } }; class rectangle:public shape { public: void getdata(double x1,double y1) { shape::getdata(x1,y1); } void displayarea() { double area=x*y; cout<<"area of rectangle"<< area; } };

void main() { clrscr(); shape *sptr; rectangle r,*rptr; triangle t,*tptr; rptr=&r; tptr=&t; rptr->getdata(20.0,30.0); tptr->getdata(20.0,30.0); sptr=&r; sptr->displayarea(); sptr=&t; sptr->displayarea(); getch(); }

Create two classes DM and DF which store the value of distance. DF store distance in metres and centimetres and DF stores distance in feet and inches. Write a program that can read values for the class opbjects and add one object of DM with another object of DF. Use the friend function to carry out the addition operation. The object that stores the result may be a DM object or a DF object. Depending upon the units in which the results are require. The display should be in feet & inches and metres & centimetres.

Create a class SHAPE, use this class to store two double type values that will be used to compute the area of the figure. Drive the class TRIANGLE and RECTANGLE from the base class SHAPE. Now add to base class a member function getdata( ) to initialize base class data members and another function displayarea( ) is to compute and display the area of figure. Make displayarea as virtual function and redefine it in the derived classes. Using these three classes design a program that accept dimensions of triangle and rectangle intrectively and display the area. Remember two values given as input will be treated as length of two sides in the case of rectangle and base & height in case of a triangle.

#include<stdio.h> #include<conio.h> #include<iostream.h> class books { public: char title[15]; char author[10]; float cost; int stock; books() { strcpy(title,"\0"); strcpy(author,"\0"); cost=0.0; stock=0; } void input() { cout<<"\n enter the title og the book"; cin>>title; cout<<"\n enter the author of the book"; cin>>author; cout<<"\n enter the cost of the book"; cin>>cost; cout<<"\n enter the stock of the book"; cin>>stock; } void output() { cout<<"\n title of the book is"<<title; cout<<"\n author of the book is"<<author; cout<<"\n cost of the book is"<<cost; cout<<"\n stock of the book is"<<stock; } }; void main() { books *b=new books[5]; int i,num; char tt[10]; char aa[10]; for(i=0;i<5;i++)

{ b->input(); b++; } cout<<"enter the title"; cin>>tt; cout<<"enter the author"; cin>>aa; cout<<"enter the quantity"; cin>>num; for(i=0;i<5;i++) { if((strcmp(b->title,tt)==0)&&(strcmp(b->author,aa)==0)&&(b->stock>=num)) { cout<<"the order is available"; cout<<"the cost would be"; cout<<b->cost *num; cout<<"the title is"<<tt; cout<<"the author is"<<aa; cout<<"the cost is"<<b->cost; cout<<"the stock is"<<b->stock; break; } else { cout<<"the required quantity of book not available"; break; } b++; } getch(); }

A book shop maintains the inventory of the books that are being sold at the shop the list includes details such as author, title & publisher and stock position. Whenever a customer wants the books, the sales person inputs the title and author and the system search the list and display whether it is available or not. If it is not, appropriate message is displayed

, if it is, then the system displays the book details and requests for the number of copies require. If the requested are available, the total cost of the require copies is displayed: otherwise the message required copies not in stock is displayed. Design a system using class called books with suitable member functions and constructors. Use new operator in constructor to allocate memory space requires.

You might also like