Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

What does class variable assignment mean ?

Posted on 2013-10-31
3
Medium Priority
?
462 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
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 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.

688 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