473,379 Members | 1,190 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,379 software developers and data experts.

Inheritance and abstract classes

Hello!

Assume you have an abstract class called Body and a derived class called
cylinder.
When you have an abstract class you can't instansiate an object.
As you can see in the abstract class there are one data member called
density that you can't access because there will not be any object of class
Body. The data member density will be inheritated and all the member
functions.
So when you have an instance of class Cylinder and you access setDensity you
will access the density that is inheritated from Body.

Now to my question: When you can't access any data member in an abstact
class is it then any point to declare any data member in such class. Why not
just declare them in the derived class instead?

class Body
{
public
virtual ~Body(){}
virtual const double getVolume() const = 0;
virtual const double getArea() const = 0;
double getDensity();
void setDensity(double);

private:
int density;
};

class Cylinder : public Body
{
public:
virtual const double getVolume();
virtual const double getArea();

private:
double radie, length;
};

//Tony
Jul 23 '05 #1
4 2273
Because all classes you want to derive from Body have a density
attribute?

Jul 23 '05 #2
Tony Johansson wrote:
Hello!

Assume you have an abstract class called Body and a derived class called
cylinder.
When you have an abstract class you can't instansiate an object.
Right.
As you can see in the abstract class there are one data member called
density that you can't access because there will not be any object of
class Body.
Well, each Cylinder is also a Body.
The data member density will be inheritated and all the member
functions.
So when you have an instance of class Cylinder and you access setDensity
you will access the density that is inheritated from Body.
Yes.
Now to my question: When you can't access any data member in an abstact
class is it then any point to declare any data member in such class. Why
not just declare them in the derived class instead?
A good thing about deriving from a class is that you inherit members so you
can put everything in the base class that is shared between all classes
derived from it. This is also true in this case. If every class derived
from Body has a density, why put it in each class separately? Just put it
once in the base class, and all the others inherit it.
Consequently, if each derived class has its own getDensity() and
setDensity(), you cannot call those functions through a pointer or
reference to Body, since Body doesn't have them. You'd always need to know
the right class and cast to that class.
class Body
{
public
virtual ~Body(){}
virtual const double getVolume() const = 0;
virtual const double getArea() const = 0;
double getDensity();
void setDensity(double);

private:
int density;
};

class Cylinder : public Body
{
public:
virtual const double getVolume();
virtual const double getArea();

private:
double radie, length;
};

//Tony


Jul 23 '05 #3

"Tony Johansson" <jo*****************@telia.com> wrote in message
news:vC*******************@newsb.telia.net...
Hello!

Assume you have an abstract class called Body and a derived class called
cylinder.
When you have an abstract class you can't instansiate an object.
As you can see in the abstract class there are one data member called
density that you can't access because there will not be any object of class Body. The data member density will be inheritated and all the member
functions.
So when you have an instance of class Cylinder and you access setDensity you will access the density that is inheritated from Body.

Now to my question: When you can't access any data member in an abstact
class is it then any point to declare any data member in such class. Why not just declare them in the derived class instead?


You certainly can access an abstract class's members and member functions.

The key is how you invoke the abstract ctor in the derived ctor's
initialisation list in order to generate a Cylinder instance.

Defining the abstract's pure-virtual member function(s) will save having to
provide that code in each derivative. The derived instance needs only call
the abstract base class's pure-virtual member functions with access
specifiers (ie: Body::getVolume()). See getVolume() in Body and Cylinder
below...

The abstract class is no longer abstract once its been derived from and an
instance of the derived type is instantiated (abstract means "i can't exist
*unless* i'm inherited"). Also, nothing prevents you from defining the body
of a pure-virtual member-function (unlike Java's interface). The abstract
concept only applies to the instantiation of an abstract type itself. Not to
a derivative.

Note the output of the allocation invocations for Body and Cylinder.
Cylinder calculates the volume for the Body base using the Cylinder's ctor's
initialization list. Now its a tad easier to then derive from Body to create
a Block or Wedge class.

#include <iostream>
using std::cout;
using std::endl;

class Body
{
double density;
double volume;
public:
Body(double d, double v)
: density(d), volume(v) // init list
{
cout << "Body ctor invoked\n";
}
virtual ~Body()
{
cout << "Body d~tor invoked\n";
}
// member functions
double getDensity() const
{
return density;
};
virtual double getVolume() const = 0
{
return volume;
};
}; // abstract class Body

class Cylinder : public Body
{
double radie;
double length;
public:
Cylinder(double d, double r, double l)
: Body(d, 3.1416 * r * r * l),
radie(r), length(l) // init list
{
cout << "Cylinder ctor invoked\n";
}
virtual ~Cylinder()
{
cout << "Cylinder d~tor invoked\n";
}
// member functions
double getVolume() const
{
return Body::getVolume(); // by access specifier
}
}; // class Cylinder

int main()
{
// Cylinder(density, radius, length)
Cylinder cylinder(1.4, 8.0, 16.0);

cout << "density = " << cylinder.getDensity();
cout << endl;
cout << "volume = " << cylinder.getVolume();
cout << endl;

return 0;
}
Jul 23 '05 #4
"Tony Johansson" <jo*****************@telia.com> wrote in message
news:vC*******************@newsb.telia.net...
Hello!

Assume you have an abstract class called Body and a derived class called
cylinder.
When you have an abstract class you can't instansiate an object.
As you can see in the abstract class there are one data member called
density that you can't access because there will not be any object of
class Body. The data member density will be inheritated and all the member
functions.
So when you have an instance of class Cylinder and you access setDensity
you will access the density that is inheritated from Body.

Now to my question: When you can't access any data member in an abstact
class is it then any point to declare any data member in such class. Why
not just declare them in the derived class instead?

class Body
{
public
virtual ~Body(){}
virtual const double getVolume() const = 0;
virtual const double getArea() const = 0;
double getDensity();
void setDensity(double);

private:
int density;
};


Now everybody who uses Body as a base class has to use your implementation
of getDensity(). What do you care how it's implemented? An abstract class
should be abstract: no constructor, no data items, and no non-virtual
methods. Of course sometimes you would like to provide a partial
implementation as a convenience. But you can have your cake and eat it too:

class Body
{
public: // note the colon
virtual ~Body() {}
virtual double getVolume() const = 0; // no need for const double
virtual double getArea() const = 0;
virtual double getDensity() const = 0; // should be const method
};

class BaseBody : public Body
{
private:
double m_density;
public:
BaseBody(double i_density=0.0) : m_density(i_density) {}
void setDensity(double i_density) {m_density = i_density;}
virtual double getDensity() const {return m_density;}
};

Now your client can use BaseBody if he is satisfied with your implementation
but can also derive directly from Body.

[snip]

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: John Perks and Sarah Mount | last post by:
Trying to create the "lopsided diamond" inheritance below: >>> class B(object):pass >>> class D1(B):pass >>> class D2(D1):pass >>> class D(D1, D2):pass Traceback (most recent call last): File...
2
by: He Shiming | last post by:
Hi, I've got a question regarding class inheritance. The following code reproduces the problem I'm dealing with: class IBase { public: virtual void Method(void)=0; };
20
by: km | last post by:
Hi all, In the following code why am i not able to access class A's object attribute - 'a' ? I wishto extent class D with all the attributes of its base classes. how do i do that ? thanks in...
20
by: Steve Jorgensen | last post by:
A while back, I started boning up on Software Engineering best practices and learning about Agile programming. In the process, I've become much more committed to removing duplication in code at a...
14
by: Steve Jorgensen | last post by:
Recently, I tried and did a poor job explaining an idea I've had for handling a particular case of implementation inheritance that would be easy and obvious in a fully OOP language, but is not at...
7
by: shintu | last post by:
Hi, For the following code snippet, the compiler complains for "mi *mi1=new mi;" statement //******************************************************* #include <iostream> using namespace std;...
4
by: vivekian | last post by:
Hi, Have this following hierarchy which am implementing for a networking program. The base class 'ASocket' is the base class from which 'AListener' and 'ATalker' inherit . None of the functions...
60
by: Shawnk | last post by:
Some Sr. colleges and I have had an on going discussion relative to when and if C# will ever support 'true' multiple inheritance. Relevant to this, I wanted to query the C# community (the...
2
by: Heinz Ketchup | last post by:
Hello, I'm looking to bounce ideas off of anyone, since mainly the idea of using Multiple Virtual Inheritance seems rather nutty. I chalk it up to my lack of C++ Experience. Here is my...
2
by: mmcgarry.work | last post by:
Hi, I would like to follow Stroustrup's advice of separating an object interface (abstract class) from an object implementation (concrete class), See Section 15.2.5 in Stroustrup 3rd Edition. ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.