Ques : Define the class Book with all the basic attributes such as title, author, publisher, price etc. Define the default constructor, member functions display_data() for displaying the Book details. Use appropriate access control specifiers in this program

I have done some part

#include<iostream.h>
#inlude<conio.h>
using namespace std;
class book
{
char author, publisher, price ,ibn;

of the program .. but i am not getting the concept of Default contructor .. Please help me in understanding it by giving an example .. it to complicated as i have readd on stackoverflow.com , on ibm website and other websites also .. but i am getting confused and confused. Please Help me ..?? and please help in solving the rest part of my program ..??

Recommended Answers

All 9 Replies

Your default constructor is the definition in which you will initialise your objects from. (in main usually)

Your default constructor looks like the following:

P.S. It is also worth noting that the class name should always be capitialised as this is a good coding practise, so, Book instead of book with that being said, your class should look like the following:

class Book {

    public:
        Book(); // Default constructor 

    private:

        char author, publisher, price ,ibn;
};

You should also make the access specifiers clear, i.e. public, private, protected

Hope this helps.

Also, specifying each element as a char means they only can have one character! I suspect that you don't want to do that... :-)

Use this (or something similar):

class Book {
    private:
        std::string author, publisher, ibn;
        double price;
    public:
        Book(); // Default constructor 
};

What goes into a default constructor? Is it an empty function? Or can we set private members with a value?
For eg.

Book(){

author = "Some Name";
}

Yes you can do the following. But, your default constructor should be left. Then, you can use method overriding to pass values through.

But, your default constructor should be left.

Should be left? Empty?

Default constructor will only exist if you do not provide your own manually hence the compiler will make a constructor for you (that's why it's called DEFAULT constructor)because in OOP manner, you cannot instantiate a new object without providing any constructor of that class.
Suppose you make an instance of class Rectangle
code:

class Rectangle
{
private:
    int width;
    int height;
public:
    Rectangle() : width(0), height(0) //initializer list
    {
    //both `width` and `height` will have an initial value of 0
    }
};

Rectangle rect; //you have just call `Rectangle()!`
//now rect.width == 0 && rect.height == 0!


hope this helps

That helped :)

First of all ... Thanxs to everyone for supporting me and giving me asnwers ..
by the answer of mark5rockzz_1 i have completly understand the question and phorce's answer has also helped me a lot.... thank u very much...!!!!

Sorry i forgot to thank rubberman... its was a good answer and i understand it..!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.