Thread: inistializing derived class veriables

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    12

    inistializing derived class veriables

    Sorry if my terminology is off, it's been a long while since i've practiced programming.

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    class parent{
    protected:
        int age;
    public:
    //setter
        void setValue(int age) { age=age; }
        parent(int);
    //getter
        int getAge() { return age; }
    //methods
        void talk() { cout<<"I am the parent, shuttup a your face! "<<endl; }
    };
    class son:public parent{
    private:
        int number;
    public:
       
    };
    parent::parent(int age){
        this->age=age;
    };
    int main () {
    
    parent dad(45);
     
    
    }
    i am trying to initialize the variable in the derived class son, i can't find a way and i have been mashing it out for a few hours now, can someone please tell me how, thanks in advance!

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    12
    is the main methods for storing and retrieving DATA in private class variables using setter/getter methods? is this common practice?

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    12

    Thumbs up

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    class parent{
    protected:
    int age;
    public:
    //setter
    void setValues(int Age) { age=Age; }
    //getter
    int getAge() { return age; }
    //methods
    void talk() { cout<<"I am the parent, shuttup a your face! "<<endl; }
    
    };
    class son:public parent{
    private:
    int number;
    public:
    void talk() { cout<<"Hi my name in John, i am the son. "<<endl; }
    son(int a) { number = a; }
    };
    
    int main () {
    
    son john(0);
    
    }
    Ok i figured it out and it makes sense to me now but i'll have to play around with this method for a few days just to let the whole process sink in, thank guys!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Base-class pointer, accessing object from derived class
    By Korhedron in forum C++ Programming
    Replies: 15
    Last Post: 09-28-2008, 05:30 AM
  2. Replies: 8
    Last Post: 03-19-2008, 03:04 AM
  3. derived class can not access base class protected member?
    By George2 in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2007, 06:32 PM
  4. Replies: 2
    Last Post: 04-06-2005, 07:25 AM
  5. Replies: 1
    Last Post: 12-11-2002, 10:31 PM