28
Nabeel Alassaf: University Of Jordan,Computer Science Department,Data Structure ١ Data Structure Instructor :Nabeel Alassaf Review Classes and Data Abstraction Lecture 3

Review lec3

Embed Size (px)

Citation preview

Page 1: Review lec3

Nabeel Alassaf: University Of Jordan,Computer Science Department,Data Structure ١

Data StructureInstructor :Nabeel Alassaf

Review Classes and Data AbstractionLecture 3

Page 2: Review lec3

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

Page 3: Review lec3
Page 4: Review lec3
Page 5: Review lec3

We can write the definition of the constructor with parameters by calling the function setTime, as follows:

Page 6: Review lec3

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.

Page 7: Review lec3

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.

Page 8: Review lec3

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:

Page 9: Review lec3

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();

}

Page 10: Review lec3

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='*';

}

Page 11: Review lec3

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='*';

}

Page 12: Review lec3

Nabeel Alassaf: University Of Jordan,Computer Science Department,Advanced Programming ١٢

testclass::testclass(double c,char d)

{x=0;y=0;z=c;ch=d;

}

Page 13: Review lec3

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:

Page 14: Review lec3

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

Page 15: Review lec3

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:

Page 16: Review lec3

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();

}

Page 17: Review lec3

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;

}

Page 18: Review lec3

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:

Page 19: Review lec3

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

Page 20: Review lec3

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

Page 21: Review lec3

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;

}

Page 22: Review lec3

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

Page 23: Review lec3

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)

Page 24: Review lec3

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

Page 25: Review lec3

Nabeel Alassaf: University Of Jordan,Computer Science Department,Data Structure ٢٥

Output:

Hi StudentGood Bye Student

Page 26: Review lec3

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)

Page 27: Review lec3

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

Page 28: Review lec3

Nabeel Alassaf: University Of Jordan,Computer Science Department,Data Structure ٢٨

Output:

Hi StudentHi StudentGood Bye StudentGood Bye Student