Solved

What does class variable assignment mean ?

Posted on 2013-10-31
3
421 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
 

Author Comment

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
why "." vs "->" 23 119
Converting to C# also VS2015 5 117
SNMP error No Such Object available on this agent at this OID 3 182
Arduino EDI - Programming language 3 79
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

932 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now