Need Help Understanding the Role of Objects in this Program

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

  1. stephon

    stephon New Member

    Joined:
    Sep 18, 2011
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    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

    Joined:
    Jan 29, 2011
    Messages:
    150
    Likes Received:
    33
    Trophy Points:
    0
    Occupation:
    Software developer
    Location:
    India
    Without objects how can you use classes??

    Please elaborate more if you want some precise answers here.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice