Default Memberwise Assignment
·
The assignment operator (=) can be used to assign
an object to another of the same type.
·
Objectsmay be passed by value to or returned by
value from functions. C++ creates a new object and uses a copy constructor to
copy the original object’s values into the new object.
·
For each class, the compiler provides a default
copy constructor that copies each member Of the original object into the
corresponding member of the new object.
What is
the output of the following program? //time.h
class
Time
{
public:
Time (int
h=12, int m=10, int s=11); //constructor with default arguments
~Time();
voidsetTime
(int h, int m, int s);
void
display();
private:
int hour;
int min; int sec;
};
----------------------------------------------------------------------------------------------------------
//t.cpp
#include
"t.h"
#include
<iostream>
using
namespace std;
Time::Time
(int h, int m, int s) { hour=h; min=m; sec=s; }
Time::~Time
() { };
void
Time::setTime (int h, int m, int s) { hour=h; min=m; sec=s; }
void
Time::display () {
cout<<hour<<":"<<min<<":"<<sec<<"
"; }
----------------------------------------------------------------------------------------------------------
//mainTime.cpp
#include
"t.h"
#include
<iostream>
using
namespace std;
void main
()
{
Time t1;
t1.display();
t1.setTime
(10, 25, 30);
Time t2;
t2 = t1;
//memberwise assignment
t2.display();
}
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.