Solved

What does class variable assignment mean ?

Posted on 2013-10-31
3
432 Views
Last Modified: 2013-10-31
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;
0
Comment
Question by:naseeam
  • 2
3 Comments
 
LVL 1

Author Comment

by:naseeam
ID: 39615479
Please discard duplicate code in above post.
0
 
LVL 1

Author Comment

by:naseeam
ID: 39615492
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.
0
 
LVL 86

Accepted Solution

by:
jkr earned 400 total points
ID: 39615549
No, sou still have both instances. Just all data members will be assigned from 'gradebook1' to another. In your case, that only works because the data is basically just a simple 'std::string', so the compiler can generate a simple assignment operator and copy constructor that take care of that. Unsually, with things getting a bit more complex (i.e. the class containing multiple data members of different complexity such as containers etc.) you will want to provide your own versions of these to have more control about what is happening behind the scenes. See also http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=367 ("Copy Constructor and Assignment Operator FAQ, Part I") and http://www.cs.umd.edu/class/spring2002/cmsc214/Projects/P1/copy-dest.html ("Copy Constructor, Assignment Operator, Destructor")
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question