Presentation is loading. Please wait.

Presentation is loading. Please wait.

Classes class A { ... };.

Similar presentations


Presentation on theme: "Classes class A { ... };."— Presentation transcript:

1 Classes class A { ... };

2 Outline Constructor Destructor C++

3 Constructor Initialization of objects using constructors
Calling the constructors if a member is an object C++

4 Initialization of Objects Using Constructors
When an object is created a constructor is called. The name of the constructor must be the same as the name of the class. Function names can be overloaded in C++, thus a class can have multiple constructors. In this case the lists of formal parameters must be different. There is no return value for constructors, and it is not allowed to use the void keyword. C++

5 Example class Person { char* name; public:
Person(); // default constructor Person(char* _name); Person(const Person& p); //copy //constructor ~Person(); void Display(); }; C++

6 Default Constructor The list a formal parameters must be empty, or if there are formal parameters then all these arguments must have a default value. If a class has a default constructor, then it is possible to define an object without giving the list of actual parameters. C++

7 If there is no Constructor
If we don’t define a constructor, then an empty default constructor will be generated automatically. If there is at least one constructor, then no default constructor will be generated, therefore if we need one we should define it. C++

8 classname(const classname & object);
Copy Constructor Purpose: define an object using another object of the same type. Form: classname(const classname & object); The const keyword is present because the actual parameter must not change. C++

9 Default Constructor Person::Person() { name = new char[1]; *name = 0;
cout << "Default constructor\n"; } C++

10 Conventional Constructor
Person::Person(char* _name) { name = new char[strlen(_name)+1]; strcpy(name, _name); cout << "Conventional constructor\n"; } C++

11 Copy Constructor Person::Person(const Person& p) {
name = new char[strlen(p.name)+1]; strcpy(name, p.name); cout << "Copy constructor\n"; } C++

12 Destructor and Display Method
Person::~Person() { cout << "Destructor\n"; delete[] name; } void Person::Display() { cout << name << endl; C++

13 The main function int main() { Person A; A.Display();
Person B("Stroustrup Bjarne"); B.Display(); Person *C = new Person("Kernighan Brian"); C->Display(); delete C; } C++

14 Output Default constructor Conventional constructor Stroustrup Bjarne
Kernighan Brian Destructor C++

15 If there is no Copy Constructor
If we don’t define a copy constructor, then a default copy constructor is generated which simply copies all the bytes of the data members (memberwise copy). This method gives good result if there are no pointers between the members. C++

16 Another main Function int main() { Person B("Stroustrup Bjarne");
B.Display(); // ... Person D(B); //the same as Person D = B; D.Display(); } If there is no copy constructor, then the destructor is called twice for the same memory location. C++

17 Example: MyVector The copy constructor The Sum member function
The main function C++

18 The Class Declaration class MyVector { private: int *elem; int dim;
public: MyVector(int *e, int d); MyVector(const MyVector & v); ~MyVector(); void SquareVect(); MyVector Sum(MyVector & v); void Display(); }; C++

19 The Copy Constructor MyVector::MyVector(const MyVector & v) {
dim = v.dim; elem = new int[dim]; for(int i=0; i < dim; i++) elem[i] = v.elem[i]; } C++

20 The Sum Member Function
MyVector MyVector::Sum(MyVector & v) { if (dim != v.dim) { cerr << "Error: different dimensions"; exit(1); } int* x = new int[dim]; C++

21 The Sum Member Function
for(int i = 0; i < dim; i++) x[i] = elem[i] + v.elem[i]; MyVector t(x, dim); delete [] x; return t; } C++

22 The main Function int main() { int x[]={1, 2, 3, 4, 5};
MyVector v1(x, 5); int y[]={2, 4, 6, 8, 10}; MyVector v2(y, 5); v1.Sum(v2).Display(); // } C++

23 Calling the Constructors if a Member is an Object
An object of a class can contain other objects as data members. Example: class C { C_1 ob_1; C_2 ob_2; C_n ob_n; }; C++

24 The Constructor In this case the constructor has the form:
C(argumentlist) : list-of-objects The list of object has the form: ob_1(arglist_1), ob_2(arglist_2), ..., ob_n(arglist_n) where argumentlist is the list of formal parameters in class C, and arglista_i is the list of arguments of the object ob_i. C++

25 Example The Pair Class husband: Person wife : Person C++

26 The Pair Class class Pair { Person husband; Person wife; public:
} C++

27 The Pair Class Pair(Person& thehusband, Person& thewife);
Pair(char* husband_name, char* wife_name) : husband(husband_name), wife(wife_name) { } void Display(); }; C++

28 Constructor Definition, and the Display Method
inline Pair::Pair(Person& thehusband, Person& thewife) : husband(thehusband), wife(thewife) { } void Pair::Display() { cout << "Husband: "; husband.Display(); cout << "Wife: "; wife.Display(); C++

29 The main function int main() { Pair XY; XY.Display(); Person A("A");
Person B("B"); Pair AB(A, B); AB.Display(); Pair UV("U", "V"); UV.Display(); } C++

30 Output Husband: Wife: Husband: A Wife: B Husband: U Wife: V C++

31 Destructor If an object is no longer used, the destructor will be executed. The name of the destructor starts with ~ and after that, the name of the class is given. There is no return value for destructors, and it is not allowed to use the void keyword. C++

32 Example #include <iostream> #include <cstring>
using namespace std; class PrintOut { char* name; public: PrintOut(char* n); ~PrintOut(); }; C++

33 Constructor PrintOut::PrintOut(char* n) {
name = new char[strlen(n)+1]; strcpy(name, n); cout << "Creating: " << name << endl; } C++

34 Destructor PrintOut::~PrintOut() {
cout << "Freeing: " << name << endl; delete name; } C++

35 Global and local objects
void MyFunction() { cout << "Calling MyFunction\n"; PrintOut local("LOCAL"); } PrintOut global("GLOBAL"); C++

36 The main function int main() { PrintOut* dynamic =
new PrintOut("DYNAMIC"); MyFunction(); cout << "Continue the main function\n"; delete dynamic; } C++

37 Output Creating: GLOBAL Creating: DYNAMIC Calling MyFunction
Creating: LOCAL Freeing: LOCAL Continue the main function Freeing: DYNAMIC Freeing: GLOBAL C++


Download ppt "Classes class A { ... };."

Similar presentations


Ads by Google