Thread: Inheritance

  1. #16
    Registered User
    Join Date
    Jan 2011
    Posts
    10
    Code:
    //---------------------------------------------------------------------------
    #include <vcl.h>
    #include<iostream>
    #include<fstream>
    #include<stdlib.h>
    #include<conio.h>
    using namespace std;
    #pragma hdrstop
    //---------------------------------------------------------------------------
    class Base{
    public:
    void Get_Array();
    int max();
    void print();
    Base();
    ~Base();
    Base(Base&);
    int& getsize();
    int*& getpointer();
    private:
    int *p, size;
    };
    //----------------------------------------------------------------------------
    class Derived:public Base{
    public:
    Derived();
    ~Derived();
    Derived(Derived& other);
    void swap(Derived& other1, Derived& other2);
    void swapsize(Derived& other2);
    void Add(Derived& other1,Derived& other2);
    Derived& operator+=(Derived& other);
    };
    //---------------------------------------------------------------------------
    #pragma argsused
    int main(int argc, char* argv[])
    {       Derived A,B;
            A.Get_Array();  B.Get_Array();
            if(A.size==B.size){
                    if(A.max()>B.max())
                    swap(A,B);
                    else A+=B;
                    A.print(); }
                    else{   Derived C(A);
                            C.size=A.size+B.size;
                            C.Add(A,B);
                            C.print(); }
            getch();
            return 0;
    }
    //---------------------------------------------------------------------------
    void Base::Get_Array(){
    cout<<"How many numbers are you going to enter: ";
       cin>>size;
       p=new int[size];
       cout<<"Enter your numbers: ";
       for(int i=0;i<size;i++)
       cin>>p[i];
       cout<<endl;}
    //---------------------------------------------------------------------------
    int Base::max(){
    int temp=p[0];
    for(int i=0;i<size-1;i++)
    if(p[i]>temp)
    temp=p[i];
    return temp;}
    //---------------------------------------------------------------------------
    void Base::print(){
    for(int i=0;i<size;i++)
    cout<<p[i]<<" ";
    cout<<endl; }
    //---------------------------------------------------------------------------
    Base::Base(){
    size=0;
    p=NULL;}
    //---------------------------------------------------------------------------
    Base::~Base(){
    delete [] p;}
    //---------------------------------------------------------------------------
    Base::Base(Base& other){
            size=other.size;
            p=new int[size];
            for(int i=0;i<size;i++)
            p[i]=other.p[i];}
    //---------------------------------------------------------------------------
    int& Base::getsize(){
    return size;}
    //---------------------------------------------------------------------------
    int*& Base::getpointer(){
    return p;}
    //---------------------------------------------------------------------------
    Derived::Derived(){
    getsize()=0;
    getpointer()=NULL;}
    //---------------------------------------------------------------------------
    Derived::~Derived(){
    }
    //---------------------------------------------------------------------------
    Derived::Derived(Derived& other){
            getsize()=other.getsize();
            getpointer()=new int[getsize()];
            for(int i=0;i<getsize();i++)
            getpointer()[i]=other.getpointer()[i];}
    //---------------------------------------------------------------------------
    void Derived::swap(Derived& other1, Derived& other2){
    int *&q=other1.getpointer();
    int *&k=other2.getpointer();
    int * temp;
    temp=q;
    q=k;
    k=temp;
    other1.swapsize(other2);}
    //---------------------------------------------------------------------------
    void Derived::swapsize(Derived& other2){
    int temp;
    temp=other2.getsize();
    other2.getsize()=getsize();
    getsize()=temp;}
    //---------------------------------------------------------------------------
    void Derived::Add(Derived& other1,Derived& other2){
    int *temp;
    temp=new int[getsize()+other1.getsize()];
    for(int i=0;i<other1.getsize();i++)
    temp[i]=getpointer()[i];
    for(int i=other1.getsize();i<2*other1.getsize();i++)
    temp[i]=other1.getpointer()[i-other1.getsize()];
    for(int i=2*other1.getsize();i<getsize()+other1.getsize();i++)
    temp[i]=other2.getpointer()[i-2*other1.getsize()];
    delete [] getpointer();
    getpointer()=temp;
    getsize()+=other1.getsize();}
    //---------------------------------------------------------------------------
    Derived& Derived::operator+=(Derived& other){
    int *&q=getpointer();
    int *&k=other.getpointer();
    for(int i=0;i<getsize();i++)
    q[i]+=k[i];
    return *this;}
    //---------------------------------------------------------------------------
    How can we access "A.size" or "B.size" or "C.size"?

  2. #17
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Either you make the relevant functions friend of the classes:
    C++ Friend Functions
    or you make size public. This is however in my opinion bad design and the correct way to do things is through getters and setters.

  3. #18
    Registered User
    Join Date
    Jan 2011
    Posts
    10
    Data members of the classes have to be private. I'm trying to learn about friend functions.

  4. #19
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675

  5. #20
    Registered User
    Join Date
    Jan 2011
    Posts
    10
    Quote Originally Posted by rags_to_riches View Post
    LoL History repeats itself. Maybe that's a dejavu for you, only for you...

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Frankly, since it does not appear to be against the rules of the game, it seems that a solution is to simply declare the global main function as a friend function in the base class definition.

    If this is just some silly bonus question, I would concentrate on the more important stuff and ignore this question. If your course consists of a whole bunch of this sort of material... you and your course mates are doomed anyway.

    Quote Originally Posted by freakyboard
    LoL History repeats itself. Maybe that's a dejavu for you, only for you...
    Now I shall make it deja vu for you. *thread closed*
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. conditional inheritance?
    By arcaine01 in forum C++ Programming
    Replies: 17
    Last Post: 09-04-2009, 04:12 PM
  2. Replies: 16
    Last Post: 06-08-2009, 03:03 PM
  3. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  4. inheritance and performance
    By kuhnmi in forum C++ Programming
    Replies: 5
    Last Post: 08-04-2004, 12:46 PM
  5. Inheritance vs Composition
    By Panopticon in forum C++ Programming
    Replies: 11
    Last Post: 01-20-2003, 04:41 AM