Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 303
  • Last Modified:

inheritance

I want to create a database.  I have 3 different levels of classes, each getting more specific.  Lets call the superclass "A".  A few different classes inherit from "A" as follows:
class a: public A{};
class b: public A{};
class c: public A{};
class d: public A{};

how can I inherit from the sub classes a,b,c,or d?

example
class thirdlevel: public a{};
class thirdlevel: public b{};
I have read a little about virtual classes but am not sure how to proceed.  Any help on this inheritance issue would be swell.
0
police45s
Asked:
police45s
1 Solution
 
Sys_ProgCommented:
Virtual classes come into picture when a class might get include twice using inheriatnce
For example

class A {
    protected :
       int a ;
};
class B : public A {
    protected :
       int b ;
};
class C : public A {
    protected :
         int c ;
};
class D : public B, public C {
    protected :
         int d ;
} ;

In the above case,
Two instances of int a would be available in class D, thus leading to ambiguity

Thus, u inherit B and C from A virtually

class A {
    protected :
       int a ;
};
class B : virtual public A {
    protected :
       int b ;
};
class C : virtual public A {
    protected :
         int c ;
};
class D : public B, public C {
    protected :
         int d ;
} ;

Now, only one instanec of A gets into D


Amit

0
 
georg74Commented:
inheritance is just automatic copying of members, while retaining type compatibility.
so, in
    class B : public A {};
b will contain all members of A and be compatible with A.
each instance of B will in fact contain an instance of A.

Amit has given you example that shows the difference between
virtual and non-virtual base classes in C++.

to get basic help on inheritance in C++, I recommend to read
few relevant chapters from a C++ book or tutorial
and read relevant on-line help files that came with you rcompiler.
search for keywords class, virtual, public, private, protected, friend, ...

if you need a hint how to organize class structure for a distinct problem,
let us know a bit more.

gl,
georg
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now