OOP Fall 2014: Constructors in C++ Week # 3 Lecture # 7
OOP Fall 2014: Constructors in C++ Week # 3 Lecture # 7
OOP Fall 2014: Constructors in C++ Week # 3 Lecture # 7
Air University
OOP
Fall 2014
Constructors in C++
Week # 3
Lecture # 7
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.
9
Example 1
10
Example 1…
11
Example 1…
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
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
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