Solved

Inheritance / Aggregation

Posted on 2004-04-15
33
374 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
  • 18
  • 15
33 Comments
 
LVL 10

Expert Comment

by:Sys_Prog
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:gbilios
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
gbilios, could u post ur BookItem class declaration as well

Amit
0
 

Author Comment

by:gbilios
Comment Utility
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
Comment Utility
Your BookItem class does not contain a Publisher object .....??????

Amit
0
 

Author Comment

by:gbilios
Comment Utility
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
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:gbilios
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
in the function you return pub?
0
 

Author Comment

by:gbilios
Comment Utility
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
Comment Utility
in the function you return pub?...........??????

didn't get your question

Amit
0
 

Author Comment

by:gbilios
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
BookItem::BookItem(const BookItem& bkObject)
     :   pub (  bkObject.pub ) , Authors(bkObject.GetAuthors()),Title(bkObject.GetTitle()),
          Year(bkObject.GetYear())
{
     
}

Amit
0
 

Author Comment

by:gbilios
Comment Utility
thanks

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

gbilios
0
 

Author Comment

by:gbilios
Comment Utility
thanks for help and consideration.

gbilios
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

744 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now