C++ Logo.wine
C++

What is constructor? Define Book class with the basic features of a book. Define the default constructor, parameterised constructor, member functions display_book_details() for displaying the details of book. Use appropriate access control specifies in this program.

What is constructor? Define Book class with the basic features of a book. Define the default constructor, parameterised constructor, member functions display_book_details() fordisplaying the details of book. Use appropriate access control specifies in this program.

Constructor
  • Constructor is a special member function of class having same name of class
  • It is called automatically when create an object.
  • It doesn’t have any return type even void.
  • It can be parameterized.
  • It can be overloaded.
  • it can has default arguments
  • It is declare as public always.
Default constructor:
constructors with no arguments or all are default arguments are called default  constructor. If a constructor is not declare in class then compiler define it implicitly is also called default constructor.
class sum
{
sum() //default constructor
or
sum(int a=3, int b=4)
}
#include<iostream.h>
#include<conio.h>
#include<string.h>
class book
{
int bid;
char bname[10],auth[10];
int price;
public:
book(int id, char *bn,char *au,int p)
{
bid=id;
strcpy(bname,bn);
strcpy(auth,au);
price=p;
}
void disp_book_details();
};
void book::disp_book_details()
{
cout<<“Book ID=”<<bid<<endl;
cout<<“Book Name=”<<bname<<endl;
cout<<“Author=”<<auth<<endl;
cout<<“Price=”<<price<<endl;
}
void main()
{
book ob(1,”Word”,”Deep”,120);
ob.disp_book_details();
getch();
}

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.