Public Group

# Inheritance Question

This topic is 4150 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Not sure if this is just an easy solution, but I am new to inheritance programming. I have some code that is as follows:


#include <string>
#include <vector>
#include <iostream>

using namespace std;

class Reference                                                 // Parent
{
private:

public:

string getTitle();
};

class Book : public Reference                                  // Derived
{
private:
string Author, Title, Publisher, Year, Volume, Series, Address;
string Edition, Month, Note, Key;
public:
void setAuthor(string author);
void setTitle(string title);
void setPublisher(string publisher);
void setYear(string year);

string getAuthor();

};

string Reference::getTitle()
{
return(Title);
}

void Book::setAuthor(string author)
{
Author = author;
}

void Book::setTitle(string title) {Title = title;} //set the book title
void Book::setPublisher(string publisher) {Publisher = publisher;} //set the book's publisher
void Book::setYear(string year) {Year = year;} //set the book year

string Book::getAuthor() {return Author;} //function to return the author

int main()
{
Book aBook;
string tempAuthor;
string tempTitle;

aBook.setAuthor("This is an author");
//aBook.setTitle("This is a title");

tempAuthor = aBook.getAuthor();
//tempTitle = aBook.getTitle();

cout << tempAuthor << endl;
cout << tempTitle << endl;

}


This code works as intended, but my question is, I am using the derived class (Book) which is a type of Reference. A book has some of the same properties as other References. Is there a way to define all of the functions in the Reference class, such as setTitle, setAuthor, setPublisher, etc ... and then allow the Book class to only be able to access a few of these functions? Is there a way to define in the Book class, which functions in the Reference class that it is allowed to access? I hope I have explained it clear enough... Thanks, Dan

##### Share on other sites
As far as I know, using vanilla constructs it is only possible to modify member access on a global scale:
class Book : protected Reference

However, I think (I don't know for sure, but I should :() you could define a public virtual member function, and overload it as private in the derived class, so it's implementation won't be accessible to users of the derived class. Is this true, anyone?

EDIT: it seems this works, but only if the user accesses the member function through the derived class:
class Reference{public:    virtual string GetTitle();};class Book : public Reference{private:    string GetTitle();};int main(){    Book vfd;                     // Object of derived type    Reference *pvfb = &vfd;       // Pointer to base type    Book *pvfd = &vfd;            // Pointer to derived type    string title;    title = pvfb->GetTitle();     // GetTitleis public    title = pvfd->GetTitle();     // C2248 error expected; GetTitle is private;    return 0;}

Another possible solution would be a proxy to a protected base member or a member of a component when aggregating:
class Reference{protected:    void BaseHiddenMember();    int BasePublicMember();};class Book : public Reference{public:    int PublicMember() { return BasePublicMember(); }};

##### Share on other sites
This should help explain things.

A reference can have any of the following fields:

Author
Title
Journal
Year
Volume
Number
Pages
Month
Note
Key
Booktitle
Editor
Organisation
Publisher
Month
Series
Edition
Institution
Type
Howpublsihed

And a book should only have these fields:

Author, Title, Publisher, Year, Volume, Series, Address, Edition, Month, Note and Key.

Is there a way to define all of the get and set functions, then in derived classes, define which ones can be used?

Dan

##### Share on other sites
Inheritance should follow a logical path.

ie. Lifefrom->Animal->Mammal->Dog->Beagle

All Beagles have every characteristics of dogs, all dogs have every characteristics of mammals, all mammals have every characteristics of animal, ect.

See the patter? If you inherit from something you should keep all the characteristics.

Your book has as much to do with reference as a rock does to a dog. So it should not be inherited from it.

theTroll

##### Share on other sites
Quote:
 Is there a way to define all of the get and set functions, then in derived classes, define which ones can be used?

No.

Inherited classes may not decrease visibility of inherited members.

What you need is composition or containment, but not inheritance.

1. 1
2. 2
3. 3
Rutin
18
4. 4
JoeJ
14
5. 5

• 14
• 10
• 23
• 9
• 48
• ### Forum Statistics

• Total Topics
632637
• Total Posts
3007577
• ### Who's Online (See full list)

There are no registered users currently online

×