Solved

Inheritance / Aggregation

Posted on 2004-04-15
33
383 Views
Last Modified: 2010-04-01
The Publisher is a part of the BookItem.

The user defined constructor in the Publisher class is initialised in the call to the user defined constructor in the BookItem class . The constructor in Publisher takes a const string object and included in the call in main. I have written the user defined constructor in the BookItem class but its not correct - obviously I get 'type expected' compile errors.
Can someone please help me?
I have included the main parts of the code.

Publisher Class
Publisher::Publisher(const string &Name) : Name(Name)
{
}
 
//main file - initialise the constructor in the BooKItem.cpp file
BookItem text1("W. Savitch", "Absolute C++ 1/e", Publisher("Addison/Wesley"), 2002);

//BookItem Class - Is this correct?
BookItem::BookItem(string Authors = "", string Title = "", Publisher(const string &theName),int Year = 0)
    {
      this->SetAuthors(Authors);
      this->SetTitle(Title);
      this->SetPublisher(Publisher(const string &theName));
      this->SetYear(Year);
   }

gbil

0
Comment
Question by:gbilios
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 18
  • 15
33 Comments
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 10839627
Here's an example

class Publisher {
          string pubName ;
      public :
          Publisher ( string name1 ) : pubName ( name1 ) {}          
}  ;

class BookItem {
         Publisher pub ;
         string bookName ;
         BookItem ( string bookName1 = "" , string pName = "" ) : pub ( pName ), bookName ( bookName1 ) {}
} ;

The Publisher constriucor gets called when u say
pub ( pName ) as shown above

HTH

Amit
0
 

Author Comment

by:gbilios
ID: 10839709
Thanks for you immediate reply..

the call in main is..

BookItem text1("W. Savitch", "Absolute C++ 1/e", Publisher("Addison/Wesley"), 2002);

The main file has a line there which calls the user defined constructor in BookItem class. There is also the call to the user defined constructor in Publisher, as you can see above.  

This is my user defined constructor in the BookItem class:

BookItem(string Authors, string Title, Publisher(const string &theName), int Year){....}

I want to know if this is correct?

gbil
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 10839749
Nope, its not

As per your requirement, this is not the way to do it
It should be the way I showed u

The BookItem Constructor as shown above does not specify a type for the third parameter
BookItem(string Authors, string Title, Publisher(const string &theName), int Year){....}

As your Publisher class has a constructor which expects a string argument, u should take a string argument as the third argument to BookItem constructor
Then u should construct the Publisher object using this parameter as I had shown above

ONe more way could be to take a Publisher type of variable as the third argument , thus it can be

BookItem(string Authors, string Title, Publisher pub, int Year) {
        // Assign pub to your BookItem class data member of type Publisher
}
Your call in main should be
BookItem text1("W. Savitch", "Absolute C++ 1/e", Publisher("Addison/Wesley"), 2002);

But this approach involves a temporry Publisher object being created and then assigned to the actual data member in your BookItem class

Amit
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:gbilios
ID: 10839783
The relationship between the BookItem and Publisher classes is aggregation, where the Publisher is the part of.  Its not direct inheritance.  However i am required to use memory dynmaic allocation, like Publisher *pub;

i forgot to mention that the user defined constructor in Publisher accepts a parameter of type string and that it is a constant, which is the name of the publisher.  The 2002 is obviously the year, so the following call in the main makes sense

BooKItem text1("Name of the Author","The Title", Publisher("Publisher's Name), int Year);

is this correct?
BookItem(string Authors = "", string Title ="", const string &theName, int Year = 0) :pub (const theName){....}

This can't be right because of the order they are called from main, since the call to the user defined constructor in Publisher is inside the paranthesis in the call in main.
i keep getting a 'type name expected' error

gbilios
0
 
LVL 10

Accepted Solution

by:
Sys_Prog earned 60 total points
ID: 10839809
gbilios,
I said there r 2 approaches

1)   Take the string argument in BookItem Constructor
In this case, the call in main should also pass a string and not a Publisher object
Thus the call in main should be  
BooKItem text1("Name of the Author","The Title", "Publisher's Name", int Year);
and constructor should be
BookItem(string Authors = "", string Title ="", const string &theName, int Year = 0) :pub (theName){....}


2)  Take a Publisher type of Argument in BookItem Constructor
In this case, the call in ain should be
BooKItem text1("Name of the Author","The Title", Publisher("Publisher's Name), int Year);
and COnstrcutor shoudl be
BookItem(string Authors = "", string Title ="", Publisher pubObject, int Year = 0) {....}


Amit
0
 

Author Comment

by:gbilios
ID: 10840079
its actually the second approach - Publisher type included in the call in main because i'm not allowed to touch the contents of the main file.  

BookItem(string Authors = "", string Title ="", Publisher pubObject, int Year = 0) {....}

0
 

Author Comment

by:gbilios
ID: 10840526
Thanks for your immediate replys.  One thing not clear to me is that how does the call to the user defined constructor in Publisher get initialised main, since there is no default constructors in either classes?
should there be extra code like :pub(const string &theName);

#ifndef PUBLISHER_H
#define PUBLISHER_H

#include <string>

using namespace std;

#define PUBLISHER_DFLT_NAME "UNKNOWN"
The Publisher class below:

class Publisher
{
      private:
            string Name;
      public:
            Publisher(const string &Name = PUBLISHER_DFLT_NAME);
            string GetName() const;
            void SetName(const string &Name);
};

extern const Publisher NullPublisher;

#endif // PUBLISHER_H

BookItem implementation

BookItem::BookItem(string Authors="",string Title="",Publisher pub,int Year=0)
{
    this->SetAuthors(Authors);
    this->SetTitle(Title);
    this->SetPublisher(.....);
    this->SetYear(Year);
} // USER DEFINED CONSTRUCTOR
}
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 10840539
The Publisher class does have a default constructor
A default constructor is one which either does not have any parameters OR all the arguments have default values
So, Publisher constructor is a default constructr

Amit
0
 

Author Comment

by:gbilios
ID: 10840842
I have to disagree with you because if you look at the constructor in the Publisher class, you will see that it accepts a parameter of type string, which means that it is a user defined constructor.  The defualt constructor is invoked if no constructors are declared or if the constructor's paranthesis is empty.  In this case of the Publisher class, the user defined constructor overides the defualt constructor.  

My next question is what to do with the Publisher pub object in the user defined constructor in BookItem.  For example

this->SetAuthors(Authors);
this->SetTitle(Title);
this->SetPublisher(pub(const string &theName); // theres gotta be a way of setting the publisher
this->SetYear(Year);
...
}
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 10840891
gbilios,

I have to say that u have read the default constructor concept form some sub-standard material

Default Constructor : A constructor which can be called without providing any arguments
      1) U do not provide any construtor, the compiler provides one for u
       2) U do provide a constructor with no arguments
      3) U provide a constructor with all the arguments with default values


Regarding your question, provide me your code for both the classes & the main function
Only then I would be able to help u out in a better way

Amit
0
 

Author Comment

by:gbilios
ID: 10841053
The Publisher class below:
#ifndef PUBLISHER_H
#define PUBLISHER_H

#include <string>

using namespace std;

#define PUBLISHER_DFLT_NAME "UNKNOWN"

class Publisher
{
     private:
          string Name;
     public:
          Publisher(const string &Name = PUBLISHER_DFLT_NAME);
          string GetName() const;
          void SetName(const string &Name);
};

extern const Publisher NullPublisher;

#endif // PUBLISHER_H

The BookItem class has only a user defined constructor

of-coarse you're right that if no constructor is declared, the C++ compiler will provide it, but in this case I have declared a user defined constructor in the Publisher class which therefore overides the default constructor.  You and I both agree that a user defined constructor is one that accepts a parameter(s).  If you look at the call in the main file you will see that the constructor in Publisher expects a parameter of type string.  Look at the Publisher file I have placed at the top of this paragraph.
//THE CALL IN THE MAIN FILE
BookItem text1("W. Savitch", "Absolute C++ 1/e", Publisher("Addison/Wesley"), 2002);

The third parameter in the BookItem class - Publisher pub.
i need some way to use the SetPublisher function to set the Publisher
The set function is written like this in the BookItem.h header file:
string SetPublisher(Publisher pub);  is this correct?

gbilios

 
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 10841142
gbilios, could u post ur BookItem class declaration as well

Amit
0
 

Author Comment

by:gbilios
ID: 10841177
The BookItem class below:

#ifndef BOOKITEM_H
#define BOOKITEM_H

#include <string>

using namespace std;

#include "Publisher.h"

class BookItem
{

private:
      string Authors;
      string Title;
      int Year;

public:
// USER DEFINED CONSTRUCTOR      
BookItem(string Authors, string Title, Publisher pub, int Year);
      

// COPY CONSTRUCTOR
      BookItem(const BookItem& bkObject);

      // ASSIGNMENT OPERATOR OVERLOADING
      BookItem operator =(const BookItem &temp);

      void RecordPurchase(const int NumberPurchased, const double &CostPrice);

//Publisher *h;

string GetAuthors() const;
string GetTitle() const;
string GetPublisher() const; // from the publisher class
int GetYear() const;

string SetAuthors(string Authors);
string SetTitle(string Title);

string SetPublisher(const string theName);

int SetYear(int Year);

~BookItem();

};
#endif
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 10841199
Your BookItem class does not contain a Publisher object .....??????

Amit
0
 

Author Comment

by:gbilios
ID: 10841281
I remed out the line Publisher *h; in the public section.

so in the in the constructor it may be

h = new Publisher(const string &theName)
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 10841298
I think it shoudl be like this ;
(I have only mentioned the changes regarding Publisher object)

class BookItem {
     ..
     ..
     Publisher *pub ;
     public :
         BookItem(string Authors, string Title, Publisher pubTemp, int Year) {
                pub = new Publisher ( pubTemp ) ;
        }    
};


Amit
0
 

Author Comment

by:gbilios
ID: 10841427
thanks for that.

if you look at the BookItem.h header file I have a function there SetPublisher()
string SetPublisher(....);

The main file contains this call to the BookItem class to set the Publisher

text1.SetPublisher(Publisher("The publisher's name"));

I made the modifications to the BookItem.cpp file as you suggested.  The above code for the
SetPublisher member function needs to be corrected.  

gbilios
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 10841574
The same code can be duplicated for the SetPublisher () function
However, since ur SetPublisher(const string theName) takes a string as a argument, foll can be done

string SetPublisher(const string theName) {
         delete pub ;        // deallocate the previously allocated memory
          pub = new Publisher ( theName ) ;             // Allocate new memory & assign the Publisher value
}

Amit
0
 

Author Comment

by:gbilios
ID: 10841621
in the main file the publisher is set by calling

SetPublisher(Publisher("Publisher's name"));

The type expected is Publisher

0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 10841654
So u can do it this way
string SetPublisher(Publisher pubTemp) {
         delete pub ;        // deallocate the previously allocated memory
          pub = new Publisher ( pubTemp ) ;           // Allocate new memory & assign the Publisher value using Copy Constructor
}


Amit
0
 

Author Comment

by:gbilios
ID: 10841776
in the function you return pub?
0
 

Author Comment

by:gbilios
ID: 10841868
one other thing

going back to the user defined constructor in BookItem, i inititialise the data members between the curly braces
pub = new Publisher(pubTemp);
this->SetAuthors(Authors);
this->SetTitle(Title);
this->SetPublisher(pub);
this->getYear(Year);
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 10847660
in the function you return pub?...........??????

didn't get your question

Amit
0
 

Author Comment

by:gbilios
ID: 10847920
So u can do it this way
string SetPublisher(Publisher pubTemp) {
         delete pub ;        // deallocate the previously allocated memory
          pub = new Publisher ( pubTemp ) ;           // Allocate new memory & assign the Publisher value using Copy Constructor
}
there is no return statement
shouldn't the function SetPublisher return a vlaue?

0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 10847931
No need of returning a value.......At the most, u could retrun a bool value indicating whether the operation of setting the Publisher was successful

Amit

0
 

Author Comment

by:gbilios
ID: 10847940
string SetPublisher(Publisher pubTemp){...}

when compiled C++ expects a return value of type string
i have to return something which is the value containing the publisher's name

i have a similary problem with the following line inside the user defined constructor in BookItem class

pub = new Publisher(pubTemp);

this->SetPublisher(pub); // should be passed inthe SetPublisher function ?
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 10847949
Why do u need both the statements in COnstructor

pub = new Publisher(pubTemp);
this->SetPublisher(pub);


Either of them would suffice

Regarding return values :
U can either change the proptotype of the SetPublisher () function to return a bool
OR
U can introduce a new function in Publisher() class which returns the name of the Publisher, Then in SetPublisher(), just return the value returned by this new function in Publisher

Amit

0
 

Author Comment

by:gbilios
ID: 10847952
you separate the new and delete operator. Chances are that , you will end up with a memory leak soon .
 

The BookItem class has an assignment operator overloading  and a ccopy constructor .
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 10847970
gbilios,

If u use the first statemmt in constructor, the destrcuotr would always be responisble to free the memory

If u use the 2nd statement in  constructor, the function itself first calls delete & then new

Amit
0
 

Author Comment

by:gbilios
ID: 10847989
So, in the copy constructor how does the Publisher pubTemp get used in the copy constructor?

The solution is to construct the
BookItem by user define and copy constructor

BookItem::BookItem(string Authors = "", string Title = "", Publisher pubTemp, int Year)

// COPY CONSTRUCTOR
BookItem::BookItem(const BookItem& bkObject)
      :Authors(bkObject.GetAuthors()),Title(bkObject.GetTitle()),
            Year(bkObject.GetYear())
{
     
}

//DESCTRUCTOR
BookItem::~BookItem()
{
   delete pub;
}
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 10847996
BookItem::BookItem(const BookItem& bkObject)
     :   pub (  bkObject.pub ) , Authors(bkObject.GetAuthors()),Title(bkObject.GetTitle()),
          Year(bkObject.GetYear())
{
     
}

Amit
0
 

Author Comment

by:gbilios
ID: 10848031
thanks

i modified the part :pub(bkObject.pub) to :pub(bkObject.GetPublisher())

gbilios
0
 

Author Comment

by:gbilios
ID: 10858194
thanks for help and consideration.

gbilios
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question