Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

What does class variable assignment mean ?

Posted on 2013-10-31
3
Medium Priority
?
471 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 1600 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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 goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.
Suggested Courses

885 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