We Can Write Member Function Out Side The Class
We Can Write Member Function Out Side The Class
We Can Write Member Function Out Side The Class
class myclass{
private:
int x; int main()
public: {
void getdata(); myclass obj;
}; obj.getdata();
void myclass :: getdata() }
{
cout<<“ enter x = ”;
cin>>x;
}
1
class Distance { Example
private:
void Distance::add_dist(Distance d2, Distance d3)
int feet;
{
float inches; inches = d2.inches + d3.inches;
public: feet = 0;
Distance() : feet(0), inches(0.0) if(inches >= 12.0)
{} {
Distance(int ft, float in) : feet(ft), inches(in) inches -= 12.0;
feet++;
{}
}
void getdist() { feet += d2.feet + d3.feet;
cout << “\nEnter feet: “; cin >> feet; }
cout << “Enter inches: “; cin >> inches;
}
void showdist() {
cout << feet << “\’-” << inches << ‘\”’;
}
void add_dist( Distance, Distance );
};
2
int main()
{
Distance dist1, dist3;
Distance dist2(11, 6.25);
dist1.getdist();
dist3.add_dist(dist1, dist2);
cout << “\ndist1 = “;
dist1.showdist();
cout << “\ndist2 = “;
dist2.showdist();
cout << “\ndist3 = “;
dist3.showdist();
cout << endl;
return 0;
}
3
Member Functions Defined Outside the
Class
4
Example : Output?
Declaring member function outside the class
Scope resolution operator
The scope operator (::) specifies the class to which the member being
declared belongs, granting exactly the same scope properties as if this
function definition was directly included within the class definition. For
example, the function set_values in the previous example has access to the
variables width and height, which are private members of class Rectangle,
and thus only accessible from other members of the class, such as this.
8
Example 1
9
Example 1…
10
Example 1…
12
Copy constructor
13
Copy Constructor
We’ve seen two ways to initialize objects.
A no-argument constructor can initialize data members to constant
values,
a multi-argument constructor can initialize data members to values
passed as arguments.
Let’s mention another way to initialize an object: you can initialize
it with another object of the same type. Surprisingly, you don’t
need to create a special constructor for this; one is already built
into all classes.
It’s called the default copy constructor.
It’s a one argument constructor whose argument is an object of the
same class as the constructor.
14
What is a copy constructor?
It is a member function which initializes an
object using another object of the same class.
A copy constructor has the following general
function prototype:
class_name (class_name&);
15
Defining copy constructors is very important
16
Copy Constructor
A copy constructor is one that takes a single
argument of the same type as the class,
passed by reference. Such that you can copy
member variable from another object of the
same class.
Syntax;
MyClass(MyClass& src);
17
Example
#include <iostream>
#include <string>
using namespace std;
class MyClass {
private:
string myName;
string myNote;
18
Example (cont’d)
public:
MyClass() {
myName = "";
myNote = "";
}
MyClass(string name, string note) {
myName = name;
myNote = note;
}
19
Example (cont’d)
//Copy Constructor
MyClass(MyClass& src)
{
Copy
myName = src.myName;
Constructor
myNote = src.myNote; Code
cout<<"Copy Constructor"<<endl;
}
void showData()
{
cout<<"Name = "<<myName<<endl;
cout<<"Note = "<<myNote<<endl;
}
};
20
Example (cont’d)
void main() {
MyClass obj1("Talal","Air University");
MyClass obj2(obj1);
obj1.showData();
Passing object
obj2.showData(); “obj1” as
Argument to Copy
} Constructor
21
Example (cont’d)
Output:
22
When do I need to write a copy constructor?
By far, the most common reason the default copy constructor
is not sufficient is because the object contains raw pointers
and you need to take a "deep" copy of the pointer.
That is, you don't want to copy the pointer itself; rather you
want to copy what the pointer points to. Why do you need to
take "deep" copies?
This is typically because the instance owns the pointer; that
is, the instance is responsible for calling delete on the pointer
at some point (probably the destructor). If two objects end up
calling delete on the same non-NULL pointer, heap corruption
results.
24
25
Difference between Copy Constructor & Assignment Operator
27
Example 3
class Distance //English Distance class
{
private:
int feet;
float inches;
public:
//constructor (no args)
Distance() : feet(0), inches(0.0)
{}
//constructor (two args)
Distance(int ft, float in) : feet(ft), inches(in)
{}
28
void getdist() //get length from user
{
cout << “\nEnter feet: “;
cin >> feet;
cout << “Enter inches: “;
cin >> inches;
}
void showdist() //display distance
{
cout << feet << “\’-” << inches << ‘\”’;
}
};
29
int main()
{
Distance dist1(11, 6.25); //two-arg constructor
Distance dist2(dist1); //one-arg constructor
Distance dist3 = dist1; //also one-arg constructor