Need Help Understanding the Role of Objects in this Program

stephon's Avatar, Join Date: Sep 2011
Newbie Member
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;
}

Last edited by shabbir; 10Nov2011 at 23:56.. Reason: Code blocks
poornaMoksha's Avatar, Join Date: Jan 2011
Ambitious contributor
Without objects how can you use classes??

Please elaborate more if you want some precise answers here.