Program to demonstrate the functioning of default, parameterized and copy constructor :-
#include<iostream.h>
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class book
{
int book_id;
float price;
char title[50];
public:
book()
{ }
book(int i, float p, char t[50])
{
book_id=i;
price=p;
strcpy(title,t);
}
book(book &b)
{
book_id=b.book_id;
price=b.price;
strcpy(title,b.title);
}
void get()
{
cout<<"\n enter book id: ";
cin>>book_id;
cout<<"\n enter title of the book: ";
gets(title);
cout<<"\n price of the book: ";
cin>>price;
}
void show()
{
cout<<"\n tilte of book: "<<title;
cout<<"\n id of book: "<<book_id;
cout<<"\n price of book: "<<price<<endl<<endl;
}
};
void main()
{
book b1,b2(1,50.5,"physics");
b1.get();
book b3(b2);
clrscr();
cout<<"\n details of b1 \n";
b1.show();
cout<<"\n details of b2 \n";
b2.show();
cout<<"\n details of b3 \n" ;
b3.show();
getch();
}
No comments:
Post a Comment