inherit class from structure

Thread Solved
Reply

Join Date: Jan 2009
Posts: 1
Reputation: kv123 is an unknown quantity at this point 
Solved Threads: 0
kv123 kv123 is offline Offline
Newbie Poster

inherit class from structure

 
0
  #1
Jan 21st, 2009
Hi,

Is this valid in C++??

struct A{...};

class B: public A{...};

Thanks!
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: inherit class from structure

 
0
  #2
Jan 21st, 2009
In C++ A Struct IS a Class. The only difference is that in a class, the members by default are private. In a Struct, they are by default public.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 5
Reputation: richard bach is an unknown quantity at this point 
Solved Threads: 0
richard bach richard bach is offline Offline
Newbie Poster
 
0
  #3
19 Days Ago
class base
{ protected:
struct node{};
};
class derived :public base
{
struct node *prev;
};

when i do this i 'm getting an error that prev is not a member of base::node.
what can i do if i want to declare an additional variable
of struct node type in class derived
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1
Reputation: YousefAB is an unknown quantity at this point 
Solved Threads: 0
YousefAB YousefAB is offline Offline
Newbie Poster
 
0
  #4
14 Days Ago
richard bach, when I compile exactly what you have, i don't get a problem.
Using Visual Studio 2008. V.9.021022.8

  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5.  
  6. class base
  7. { protected:
  8. struct node{};
  9. };
  10. class derived :public base
  11. {
  12. struct node *prev;
  13. };
  14.  
  15.  
  16. class A
  17. {
  18. public:
  19. int a;
  20. };
  21.  
  22. struct B : A { };
  23.  
  24. struct C
  25. {
  26. public:
  27. int c;
  28. };
  29.  
  30. class D : public C { };
  31.  
  32. int main()
  33. {
  34. B b;
  35. D d;
  36. b.a = 1;
  37. d.c = 2;
  38. cout << b.a << " " << d.c << endl;
  39. }

I compile that and no errors, and I do actually get the numbers.
I don't know to what extend this works, but to this, it is working.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC