Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

We Can Write Member Function Out Side The Class

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 30

Function's 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.

The only difference between defining a member function completely


within the class definition or to just include its declaration in the function
and define it later outside the class, is that in the first case the function is
automatically considered an inline member function by the compiler, while
in the second it is a normal (not-inline) class member function. This causes
no differences in behavior, but only on possible compiler optimizations.
Overloading Constructors
Like any other function, a constructor can also
be overloaded

With different number of parameters


Parameters of different types.
The compiler will automatically call the one whose
parameters match the arguments:

8
Example 1

9
Example 1…

Two Objects of class Rectangle is constructed


rect  default constructor
rectb is a constructor with two parameters

The way of calling constructors by enclosing


their arguments in parentheses, is known as
functional form

10
Example 1…

 The default constructor is the constructor that


takes no parameters
It is special because it is called when an object
is declared
But not initialized with any arguments.
Look default constructor (Previous Lecture)
11
Example 2

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

In the absence of a copy constructor, the C+


+ compiler builds a default copy
constructor for each class which is doing a
member wise copy between objects.
Default copy constructors work fine unless
the class contains pointer data members ...

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?

First, you should understand that if you do not


declare a copy constructor, the compiler gives
you one implicitly.
The implicit copy constructor does a member-
wise copy of the source object.
 For example, given the class: In many cases,
this is sufficient. However, there are certain
circumstances where the member-wise copy
version is not good enough.
23
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

The difference between the assignment operator


and the copy constructor is that

The copy constructor actually creates a new


object before copying data from another object
into it.

Whereas, the assignment operator copies data


into an already existing object.
26
Example 2

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

//display all lengths


cout << “\ndist1 = “; dist1.showdist();
cout << “\ndist2 = “; dist2.showdist();
cout << “\ndist3 = “; dist3.showdist();
cout << endl;
return 0;
}
30

You might also like