473,388 Members | 1,375 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

C++ Both class should inherit same base class. Add methods

7
Class book
{
private:
char title [30];
char author [];};
Feb 8 '14 #1
10 1537
weaknessforcats
9,208 Expert Mod 8TB
There's not enough information for me to know what your question is. Please post additional details.
Feb 8 '14 #2
Mobola
7
@weaknessforcats
what happen is that from the two class definitions, using generalization, put them into appropriate classification hierarchy si that they both inherit from a common base class. Add appropriate methods. I posted the two codes earlier. Help please
Feb 8 '14 #3
weaknessforcats
9,208 Expert Mod 8TB
You showed only one class. You don't have an inheritance situation unless you have at least one more class.

What are the other classes?
Feb 8 '14 #4
Mobola
7
@weaknessforcats
there were two classes. The first one is class book, and the second one was class magazine. Below

class magazine
{
private:
char title [30];
char editor [30];
char publisher [30];
};
Feb 8 '14 #5
weaknessforcats
9,208 Expert Mod 8TB
OK.

Look at the Magazine and the Book. If there are common data members you could put them in a third class called, say, Publication and take them out of Magazine and Book.

Magazine derives from Publication.
Book derives from Publication.

Next, write member functions in Publication to change the data and to show the data.

Since Book derives from Publication, when you create a Book object you can use the Publication methods with the Book object because Book IS-A Publication.

In memory you have only one object, Book, and part of that object is an embedded Publication object.

The idea is that when you create a Magazine object there will be an embedded Publication object there also. What you have is ONE SET of functions for the Book and the Magazine as opposed to two sets of functions, one for each class. Reuse of code is a prime C++ objective.
Feb 8 '14 #6
Mobola
7
Thank you so much. But please is there a way you can tip me on the code for publication. Like shoud it be private or public. I mean can i have something like this....

{
Class publication: private class book: private class magazine

{
private:
char title [30];
char publisher [30];

setdisplay() {title};
setdisplay() {publisher};

};

{
cout<<''enter title"<<
cout<<''enter publisher"<<
};

};

please correct me. Thank you
Feb 9 '14 #7
weaknessforcats
9,208 Expert Mod 8TB
The data members should always be private. The member functions in this case should be public. You should inherit public so that everything that is public in the base class will be public in the derived class.

Do not inherit private or protected until you know what those are for. I can tell you they do not apply here.

Think about what you are doing. The publication class is not a derived class so it doesn't inherit from anything. The Magazine IS A Publication so it derives from Publication. The Book IS A Publication so it derives from Publication.

I can't tip you the code because I know how to do this. You will too but only if you do it yourself. Take your best shot and post again. We'll get through this.
Feb 9 '14 #8
Mobola
7
Ok, like this....

Class publication

{
public:
char title [30];
char author [30];
char editor [30];
char publisher [30];
char ISBN [20];
};

class book: public publication

{
private:
char title [30];
char author [30];
char publisher [30];
char ISBN [20];
};

class magazine: public publication

{
private:
char title [30];
char editor [30];
char publisher [30];
};


{Edit: Request to write code removed}
Feb 9 '14 #9
weaknessforcats
9,208 Expert Mod 8TB
Close.

Remember the derived class inherits from the base class. Therefore, the derived class does not have any data members that duplicate data members in the base class.

In your case, the base class Publication does not have an ISBN member because not all publications have an ISBN. Only books do. The same applies to author. So remove these from the base class:

Expand|Select|Wrap|Line Numbers
  1. Class publication
  2.  
  3.  { 
  4.  public:
  5.  char title [30];
  6.  char editor [30];
  7.  char publisher [30];
  8.   };
The book class does not need title, editor or publisher because title and publisher are inherited from publication. Books don't have editors so now editor needs to be removed from publication. Now things look like:

Expand|Select|Wrap|Line Numbers
  1. Class publication
  2.  
  3.  { 
  4.  public:
  5.  char title [30];
  6.  char publisher [30];
  7.   };
  8.  
  9.  class book: public publication
  10.  
  11.  {
  12.  private:
  13.  
  14.   char author [30];
  15.   char ISBN [20];
  16.  };
  17.  
  18.  
Finally, the magazine class has an editor, a title, and a publisher. The title and the publisher come from the publication class and the editor becomes a member of the magazine class:

Expand|Select|Wrap|Line Numbers
  1. class magazine: public publication
  2.  
  3.  {
  4.  private:
  5.   char editor [30];
  6.  };
  7.  
You should be able to see that there are no duplications of data members and that each class either has the data or inherits the data it needs.

Does any of this make sense to you?
Feb 9 '14 #10
Mobola
7
Yes. It does. Thank you. I need to note that, a derived class does not duplicate its member of thesame member of the base class, it inherit. Thank you
Feb 9 '14 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: Tim Clacy | last post by:
Your expertise will be appreciated... Here's a general templatised class; all specialisations of this class should have a pointer to a specialisation of the same, templatised type: ...
9
by: Banaticus Bart | last post by:
I wrote an abstract base class from which I've derived a few other classes. I'd like to create a base class array where each element is an instance of a derived object. I can create a base class...
4
by: Nataraj M | last post by:
Hi, I just don't want anybody derive a class from my class. For example: /////////////////////// //MY CODE class MyClass { .... }; ///////////////////////
6
by: Alf P. Steinbach | last post by:
#include <iostream> struct Belcher { int x; void belch() { std::cout << x << std::endl; } Belcher( int anX ): x( anX ) {} }; struct Person: private Belcher { Person(): Belcher( 666 ) {} };
1
by: Bill Menees | last post by:
I've got a RichTextBoxEx inherited from RichTextBox, and I want to overload the TextLength property to use TextBoxBase's implementation of TextLength. But since TextBoxBase isn't my immediate base...
8
by: TS | last post by:
I am trying to get set a property of a control on the inherited class from base class. I imagine i have to use reflection, so could someone give me the code to do it? something like this?...
1
by: Jonas | last post by:
Hi! I have a base class called ListBase.vb, from which I derive EmplList.ascx.vb. In EmplList.ascx.vb I declared a System.Web.UI.WebControls.DataGrid. Now I want to add functionality to the...
2
by: Stephan Hoffmann | last post by:
Hi, I'm new to std::auto_ptr<> and wanted to use it with a base class and several derived classes. I'm using gcc 3.3.5 and get a compile error I don't know how to resolve when compiling the...
4
by: ABC | last post by:
I want define a base class as: class abc { class cde { } } when I inhert abc class,
15
by: Anthony Greene | last post by:
This is probably a very introductory object-oriented question, but it has been nagging me for years, and since I've never been able to find the right answer, I've had to work around it with...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.