473,715 Members | 2,860 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

7 New Member
Class book
{
private:
char title [30];
char author [];};
Feb 8 '14 #1
10 1562
weaknessforcats
9,208 Recognized Expert Moderator Expert
There's not enough information for me to know what your question is. Please post additional details.
Feb 8 '14 #2
Mobola
7 New Member
@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 Recognized Expert Moderator Expert
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 New Member
@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 Recognized Expert Moderator Expert
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 New Member
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 Recognized Expert Moderator Expert
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 New Member
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 Recognized Expert Moderator Expert
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

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

Similar topics

12
2566
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: template<typename T, const int N> struct Base : T { typedef Base<T, N> Type;
9
4994
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 pointer which points to an instance of a derived class, but when I pass that base class pointer into a function, it can't access the derived object's public functions. Although, the base class pointer does call the appropriate virtual function...
4
1841
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
1682
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
14133
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 class, I don't know how to call its TextLength property. Any ideas? public override int TextLength { get { return base.TextLength; //Uses RichTextBox.TextLength, which I
8
3078
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? this.GetType().GetMember("panel1").SetValue(xx).Left = 44;
1
997
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 base class ListBase.vb that can access the DataGrid object. How can I accomplish this "backwards" inheritance? Brgds
2
4575
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 following (in file testInteger.cc): line 38: std::auto_ptr<HInteger> d(new HInteger(5)); line 39: std::auto_ptr<HObject> e(new HObject());
4
1653
by: ABC | last post by:
I want define a base class as: class abc { class cde { } } when I inhert abc class,
15
7367
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 non-optimal code. However, I suspect there must be a proper object-oriented approach to solving this problem. Perhaps someone might be able to help me out. Oftentimes I need to convert an object declared as a base type into a object of one of its...
0
8821
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8718
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9103
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9047
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7973
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6646
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4738
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3175
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2118
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.