Link to home
Start Free TrialLog in
Avatar of naseeam
naseeamFlag for United States of America

asked on

What does class variable assignment mean ?

int x = 4;
int y = 7:

y = x;    // now    y   contains  4

This isn't true for class variable assignments.  In class variable assignments, members of one class aren't assigned to members of another class.  Then, what does it mean to assign one class variable to another ?

#include <iostream>
#include <string>


class GradeBook
{
public:
   GradeBook( std::string );
   GradeBook() {};
   void setCourseName( std::string );
   std::string getCourseName();
   void displayMessage();
   std::string courseName;
   
};


#include <iostream>
#include <string>


class GradeBook
{
public:
   GradeBook( std::string ); 
   GradeBook() {}; 
   void setCourseName( std::string ); 
   std::string getCourseName(); 
   void displayMessage();
   std::string courseName; 
    
};


int main()
{

  GradeBook gradebook1, another;
  
  gradebook1.courseName = "CS101";
  another.courseName = "CS200";
  
  another = gradebook1;

  
  std::cout << "\n" << "Course Name is: " << another.courseName << "\n";
  
  return 0;
}

Open in new window


Program output is:    Course Name:  CS101

What does the following assignment statement mean ?
another = gradebook1;
Avatar of naseeam
naseeam
Flag of United States of America image

ASKER

Please discard duplicate code in above post.
Avatar of naseeam

ASKER

another = gradebook1;

I think this assignment statment shouldn't be allowed.  Now we have lost one instance of class.  We cannot get access to it's members.
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial