Solved

What does class variable assignment mean ?

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

760 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

19 Experts available now in Live!

Get 1:1 Help Now