1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Need Help Understanding the Role of Objects in this Program

Discussion in 'C++' started by stephon, Nov 10, 2011.

  1. stephon

    stephon New 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 a moderator: Nov 10, 2011
  2. poornaMoksha

    poornaMoksha New Member

    Without objects how can you use classes??

    Please elaborate more if you want some precise answers here.
     

Share This Page