Classes

This is a discussion on Classes within the C++ Programming forums, part of the General Programming Boards category; Ohhh I don't know about that. Ever heard of a "struct hierarchy"? Neither have I... structures don't facilitate object oriented ...

  1. #16
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Ohhh I don't know about that. Ever heard of a "struct hierarchy"? Neither have I... structures don't facilitate object oriented concepts like inheritance and polymorphism like classes do.
    Well, I have to admit I've never tried using a "struct hierarchy" in C++, but if the following code works, then in C++ there's no difference between classes and structs accept for the default access:
    Code:
    include <iostream>
    using namespace std;
    
    struct StructA
    {
    private:
    	int a_number;
    public:
    	StructA(int num): a_number(num){};
    	
    	int Get_a()
    	{
    		return a_number;
    	}
    	
    	virtual void print()
    	{
    		cout<<"This is a."<<endl;
    	}
    };
    
    struct StructB: public StructA
    {
    private:
    	double b_number;
    public:
    	StructB(double num1, int num2 ): StructA(num2)
    	{
    		b_number = num1;
    	}
    	
    	double Get_b()
    	{
    		return b_number;
    	}
    	
    	virtual void print()
    	{
    		cout<<"This is b."<<endl;
    	}
    };
    
    int main()
    { 
    	StructB my_b(5.1, 1);
    	
    	cout<<my_b.Get_a()<<endl
    		<<my_b.Get_b()<<endl;
    	
    	
    	StructA my_a(10);
    
    	StructA* pStructA = &my_a;
    	pStructA->print();
    	
    	pStructA = &my_b;
    	pStructA->print();
    
    	return 0;
    }
    Last edited by 7stud; 05-03-2003 at 03:53 AM.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 10:47 AM
  2. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 08:04 AM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  4. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 04:38 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21