Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Need Help Understanding the Role of Objects in this Program (http://www.go4expert.com/forums/help-understanding-role-objects-program-t27140/)

stephon 10Nov2011 22:25

Need Help Understanding the Role of Objects in this Program
 
Code:

#include <iostream>
#include <cmath>
#include <fstream>
#include <cstdlib>
#include <string>

using namespace std;
// A: define variableone
// B: define string name


class one

{
    public:
                one(int a); // A2: this is the declaration of a fubction that will be used to give variableone a value
                void printvariableone();

        private:
                int variableone; // A1: it starts here with a declaration
};

class two

{
    public:
                two(string x, one oneobject); // B2: the declaration of a function that will define the string and a class one object
                void printstring();

        private:
                string name; // B1: this declares a string called name
                one oneref; // B1A: an object to reference class one
};

one::one(int a) // A3: this will set variableone to the value of a, which will be given in main
    {variableone = a;}

// to print the stuff
void one::printvariableone()
    {cout << variableone << endl;}

two::two(string x, one oneobject) // B3: this member initializing sets the string x to the string name and the object to oneref
: name(x), oneref(oneobject)
{}



void two::printstring()
    {
        cout << name << endl;
        oneref.printvariableone();
    }

int main()
{
        one mainObj(6); // A4: apparently this passes 6 to the class one constructor via an object

        two peopleobject("Stephon", mainObj); // this line uses an object to reference class two constructor and pass a string and an object

        // to print both
        peopleobject.printstring();


        return 0;
}


poornaMoksha 16Nov2011 17:03

Re: Need Help Understanding the Role of Objects in this Program
 
Without objects how can you use classes??

Please elaborate more if you want some precise answers here.


All times are GMT +5.5. The time now is 17:20.