Date post: | 06-Aug-2015 |
Category: |
Art & Photos |
Upload: | tamer-abu-alzenat |
View: | 37 times |
Download: | 2 times |
Nabeel Alassaf: University Of Jordan,Computer Science Department,Data Structure ١
Data StructureInstructor :Nabeel Alassaf
Review Classes and Data AbstractionLecture 3
Nabeel Alassaf: University Of Jordan,Computer Science Department,Data Structure ٢
Constructors
• Use constructors to guarantee that data members of a class are initialized
• Two types of constructors:
− With parameters
− Without parameters
• Constructor without parameters is called the default constructor
We can write the definition of the constructor with parameters by calling the function setTime, as follows:
Nabeel Alassaf: University Of Jordan,Computer Science Department,Data Structure ٦
Invoking a Constructor
• A constructor is automatically executed when a class variable is declared
• To invoke the default constructor:
The statement:clockType yourClock;declares yourClock to be an object of type clockType. In this case, the default constructor executes and the member variables of yourClock are initialized to 0.
where argument1, argument2, and so on, is either a variable or an expression.
Note the following:• The number of arguments and their type should match the
formal parameters (in the order given) of one of the constructors.
• If the type of the arguments does not match the formal parameters of any constructor (in the order given), C++ uses type conversion and looks for the best match. For example, an integer value might be converted to a floating-point value with a zero decimal part. Any ambiguity will result in a compile-time error.
Nabeel Alassaf: University Of Jordan,Computer Science Department,Advanced Programming ٨
#include<iostream>using namespace std;class testclass{public:
void print() const;testclass();testclass(int,int);testclass(int,int,double);testclass(double,char);
private:int x;int y;double z;char ch;
};
Example:
Nabeel Alassaf: University Of Jordan,Computer Science Department,Advanced Programming ٩
void main(){
testclass one;testclass two(5,6);testclass three(5,7,4.5);testclass four(4,9,12);testclass five(3.4,'D');
one.print();two.print();three.print();four.print();five.print();
}
Nabeel Alassaf: University Of Jordan,Computer Science Department,Advanced Programming ١٠
void testclass::print() const{
cout<<"x= "<<x<<" y="<<y<<" z="<<z<<" ch="<<ch<<endl;
}testclass::testclass(){
x=0;y=0;z=0;ch='*';
}
Nabeel Alassaf: University Of Jordan,Computer Science Department,Advanced Programming ١١
testclass::testclass(int a,int b)
{x=a;y=b;z=0;ch='*';
}testclass::testclass(int a,int b,double c)
{x=a;y=b;z=c;ch='*';
}
Nabeel Alassaf: University Of Jordan,Computer Science Department,Advanced Programming ١٢
testclass::testclass(double c,char d)
{x=0;y=0;z=c;ch=d;
}
Nabeel Alassaf: University Of Jordan,Computer Science Department,Advanced Programming ١٣
x= 0 y=0 z=0 ch=*x= 5 y=6 z=0 ch=*x= 5 y=7 z=4.5 ch=*x= 4 y=9 z=12 ch=*x= 0 y=0 z=3.4 ch=D
OUTPUT:
If you replace the constructors of the classclockType with the constructor in Line 1 (the constructor with the default parameters), then you can declare clockType objects with zero, one, two, or three arguments as follows:
clockType clock1; //Line 2clockType clock2(5); //Line 3clockType clock3(12, 30); //Line 4clockType clock4(7, 34, 18); //Line 5
Nabeel Alassaf: University Of Jordan,Computer Science Department,Advanced Programming ١٥
#include<iostream>using namespace std;class testclass{public:
void print() const;testclass(int=0,int=0,double=0.0,char='*');
private:int x;int y;double z;char ch;
};
Example:
Nabeel Alassaf: University Of Jordan,Computer Science Department,Advanced Programming ١٦
void main(){
testclass one;testclass two(5,6);testclass three(5,7,4.5);testclass four(4,9,12);testclass five(0,0,3.4,'D');
one.print();two.print();three.print();four.print();five.print();
}
Nabeel Alassaf: University Of Jordan,Computer Science Department,Advanced Programming ١٧
void testclass::print() const{
cout<<"x= "<<x<<" y="<<y<<" z="<<z<<" ch="<<ch<<endl;
}
testclass::testclass(int a,int b,double c,char d)
{x=a;y=b;z=c;ch=d;
}
Nabeel Alassaf: University Of Jordan,Computer Science Department,Advanced Programming ١٨
x= 0 y=0 z=0 ch=*x= 5 y=6 z=0 ch=*x= 5 y=7 z=4.5 ch=*x= 4 y=9 z=12 ch=*x= 0 y=0 z=3.4 ch=D
OUTPUT:
Nabeel Alassaf: University Of Jordan,Computer Science Department,Advanced Programming ١٩
Classes and Constructors: Notes
• If a class has no constructor(s)
− C++ automatically provides the default constructor
− However, object declared is still uninitialized
• If a class includes constructor(s) with parameter(s) and does not include default constructor
− C++ does not provide default constructor
Nabeel Alassaf: University Of Jordan,Computer Science Department,Advanced Programming ٢٠
#include<iostream>using namespace std;
class test1{public:
void print() const;test1(int z,int w);
private:int x;int y;
};void main(){
test1 object1; test1 object2(10,20);
Example:
è illegalè legal
Nabeel Alassaf: University Of Jordan,Computer Science Department,Advanced Programming ٢١
object1.print();object2.print ();
}
void test1::print() const{
cout<<"x= "<<x<<" y="<<y<<endl;}
test1::test1(int z,int w)
{x=z;y=w;
}
Nabeel Alassaf: University Of Jordan,Computer Science Department,Data Structure ٢٢
Destructors
• Destructors are functions without any type• The name of a destructor is the character '~'
followed by class name• The name of the destructor clockType:
~clockType();• A class can have only one destructor
− It has no parameters• The destructor is automatically executed when
the class object goes out of scope
Nabeel Alassaf: University Of Jordan,Computer Science Department,Data Structure ٢٣
#include<iostream>using namespace std;
class Test{public:
void print();Test();~Test();
private:int x,y;
};
void main(){
Test object1;}
Example1:( check this code)
Nabeel Alassaf: University Of Jordan,Computer Science Department,Data Structure ٢٤
void Test::print(){
cout<<x<<" "<<y;}
Test::Test(){
cout<<"Hi Student"<<endl;}Test::~Test (){cout<<"Good Bye Student"<<endl;}
Nabeel Alassaf: University Of Jordan,Computer Science Department,Data Structure ٢٥
Output:
Hi StudentGood Bye Student
Nabeel Alassaf: University Of Jordan,Computer Science Department,Data Structure ٢٦
#include<iostream>using namespace std;
class Test{public:
void print();Test();~Test();
private:int x,y;
};
void main(){
Test object1[2];}
Example2:( check this code)
Nabeel Alassaf: University Of Jordan,Computer Science Department,Data Structure ٢٧
void Test::print(){
cout<<x<<" "<<y;}
Test::Test(){
cout<<"Hi Student"<<endl;}Test::~Test (){cout<<"Good Bye Student"<<endl;}
Nabeel Alassaf: University Of Jordan,Computer Science Department,Data Structure ٢٨
Output:
Hi StudentHi StudentGood Bye StudentGood Bye Student