Sei sulla pagina 1di 26

Department of Computer Science & Engineering

Air University

OOP
Fall 2014
Constructors in C++
Week # 3
Lecture # 7

By: Saqib Rasheed

1
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;
}

2
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)
{} {
inches -= 12.0;
Distance(int ft, float in) : feet(ft), inches(in)
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 );
};
3
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;
}
4
Member Functions Defined Outside
the Class

5
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:

9
Example 1

10
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

11
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)

12
Example 2

13
Copy 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
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.

23
Example 2

24
Assignment
 Define a class ‘date’ with data members year, month and day with
member functions SetDate and GetDate for input and output
respectively of class objects.
 Define a default constructor as well as a user defined constructor,
default constructor will not set any values of objects, whereas user
defined constructor will take three arguments and will set values of
day, month and year.
 Declare two objects, set the values of day, month and year either
through constructor or using setdata function and then compare
these two dates in a member function ‘compare’ then display the
difference of two dates in days in main function.
 Data members should be declared as private and member functions
will be public, divide

25
Assignments..
Due Date 23-09-14 (Next Class)
Hard Copies (Print)
Late assignment will not be accepted
Copy assignment will be marked zero (both)
Do your own work.

26

Potrebbero piacerti anche