The C++ programming language

Slides:



Advertisements
Similar presentations
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Advertisements

Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
Review of C++ Programming Part II Sheng-Fang Huang.
OOP Languages: Java vs C++
Introduction to C++. Overview C++? What are references Object orientation Classes Access specifiers Constructor/destructor Interface-implementation separation.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Classes Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd Spetember 2006.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
1 Chapter 10: Data Abstraction and Object Orientation Aaron Bloomfield CS 415 Fall 2005.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
Copyright  Hannu Laine C++-programming Part 3 Hannu Laine.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Inheritance Session 5 Object Oriented Programming with C++/ Session 5/ 1 of 41.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 4 – August 30, 2001.
More C++ Features True object initialisation
Chapter 9 Classes: A Deeper Look, Part I Part II.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Learners Support Publications Constructors and Destructors.
LThe C++ programming language Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 4./0. lThe object oriented view of the world.
LThe C++ programming language Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 5./0. lExample on definition of an object having.
Memory Management.
Constructors and Destructors
Eine By: Avinash Reddy 09/29/2016.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Procedural and Object-Oriented Programming
The C++ programming language
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Andy Wang Object Oriented Programming in C++ COP 3330
Java Primer 1: Types, Classes and Operators
Review: Two Programming Paradigms
Introduction to Classes
Constructor & Destructor
Constructors & Destructors
Class: Special Topics Copy Constructors Static members Friends this
Programmazione I a.a. 2017/2018.
This pointer, Dynamic memory allocation, Constructors and Destructor
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Introduction to Classes
Chapter 15 Pointers, Dynamic Data, and Reference Types
CS212: Object Oriented Analysis and Design
10.2 const (Constant) Objects and const Member Functions
Operators.
Constructors and Other Tools
Constructors and destructors
Constructors and Destructors
The C++ programming language
The Destructor and the Assignment Operator
9-10 Classes: A Deeper Look.
The C++ programming language
The C++ programming language
Java Programming Language
The C++ programming language
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Destructors, Copy Constructors & Copy Assignment Operators
Pointers, Dynamic Data, and Reference Types
Destructors, Copy Constructors & Copy Assignment Operators
More C++ Classes Systems Programming.
9-10 Classes: A Deeper Look.
Video: The Sound of Raining
Classes Class Data Members & Initializers
Introduction to Classes and Objects
Presentation transcript:

The C++ programming language Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 28./0. The C++ programming language The basics of object oriented programming in C++ Constructor and destructor Example on definition of an object having initialising parameter Inheritance of class properties to subclasses, instances Example program on class hierarchy Calling order of constructors Calling order of destructors Remarks on example program

Constructor and destructor Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 28./1. Constructor and destructor The objects of real world come into being and come to an end. It may be needed to set certain default values, make certain starting steps at birth of object. It may be likewise needed to perform certain operations at the moment of cessation of the object in order to clear it without any trace. Let us recall our earlier class example: class List {private : int count; char names[300][25]; // Fixed amount of reserved memory!? public : ... }; From the declaration of the class is evident that an object of this type will always allocate space for 300 names not depending on the real requirements: if we need more or less. This problem can be avoided easily if the actual value of count and hereby the amount of required memory is given at creation of object so at definition of object-variable. To achieve this goal the next form of class declaration is required:

t_name * names; // Here is given a pointer that points to the names. Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 28./2. typedef char t_name[25]; //This type for names makes easy to modify the program later class List {private : int count; t_name * names; // Here is given a pointer that points to the names. public : List( int count0 = 0 ); // The declaration of the constructor function. // The count0 parameter will give the number of required // elements when an instance of the class will be created. // If the parameter is missed at calling then the count0 will be equal to 0 ~List( ); // the declaration of the destructor function, // it has parameter never ! … // here are the other member functions mentioned earlier }; // The definitions of the constructor and destructor: List :: List( int count0 ) // To give again the 0 default value here is prohibited! {count = count0 ; names = new t_name[count0 ]; // Allocating the required memory space dinamically for ( int i = 0; i< count; i++) names[ i ][0]= '\0'; //The elements will be empty strings. } List :: ~List( ) { if (names) delete[ ] names; } // Freeing the dinamically allocated space.

List lists[25]; // A vector including 25 List-type objects. Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 28./3. Example on definition of an instance - object variable - of the List class that waits for a starting value for the parameter: List dwarfs(7); // Creation of a List-type object for storing the names of seven dwarfs. If all parameters of the constructor get default value then the absence of actual values at every instances does not result problem at defining arrays of class type elements: List lists[25]; // A vector including 25 List-type objects. If one of the parameters has not default value then a new constructor without parameters has to be applied in the class: class List { … public: List( int count0 ) ; //This constructor has not default parameter value. List( ){ } // A second - default - constructor without parameter. … }; List lists[25]; // In such a case the default constructor will be invoked.

Remarks on constructors: Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 28./4. Remarks on constructors: If there is not given constructor then the compiler creates a hidden default one. A constructor given by the programmer can resolve the problem of passing default value to the object (object initialisation). A constructor has not a return type or value declared even it has not a void type. If the programmed constructor has not parameters then the empty parentheses at definition of objects (instances) can be left:    E.g.: List2 :: List2( ) {...} Using such a class definition            List2 listObject1( ), listObject2; object definitions are valid. A class may have more than one constructors if they have different parameter signature. If the class type is intended for definition of arrays default constructor is needed. It can be automatic; programmed without parameters or programmed having only parameters with default parameter values. Dynamic allocation of objects:       List * dwarfsPtr = new List(7); List * listsPtr = new List[25]; freeing: delete dwarfsPtr ; delete[ ] listsPtr ;

Inheritance of class properties to subclasses, instances Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 28./5. Inheritance of class properties to subclasses, instances count names[ ] SetCount() GetCount() SetElement() GetElement() class instance List The List class is the base class or ancestor of Group class. The Group class is the subclass or descen-dant of List class. Let us inherit from the base class List the class Group that do has a new data member besides the inherited data members and member functions : groupNo and two new member functions to set and get the value of this new data member: SetGroupNo( ) and GetGroupNo( ). groupNo SetGroupNo() GetGroupNo() Group GK201 GK301 Let us create the two objects or instances that fall under Group class : GK201 and GK301 study groups!

Example program on class hierarchy Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 28./6. Example program on class hierarchy #include <stdio.h> #include <conio.h> #include <string.h> typedef char t_name[25]; // User defined type for storing a name class List { protected: int count; t_name * names; public: List(int count0 = 0); // Constructor, having default 0 count of elements. ~List( ); // Destructor. void SetCount(int countIn) {count = countIn; } int GetCount( ) { return count; } void SetElement(int i, char* nameIn) {strcpy(names[ i ], nameIn); } void GetElement(int i, char* nameOut ) { strcpy(nameOut, names[ i ]); } }; // The definitions for constructor and destructor are given at the end of program.

{ clrscr(); puts("Creation and usage of Group objects\n\n"); Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 28./7. class Group : public List // Inheriting the class Group from the class List { protected: char groupNo[ 6 ]; // E.g.: "GK201" public: Group(int count0 = 0, char * groupNo0 = " " ); // Constructor, // having 0 default count value and empty string as default group number. ~Group( ){ }; // Destructor. void SetGroupNo(char* groupNoIn) {strcpy(groupNo, groupNoIn); } void GetGroupNo(char* groupNoOut) {strcpy(groupNoOut, groupNo); } }; // The definition of the constructor is given at the end of program. // Defining object variables: Group GK201(5, "GK201"), GK301(8, "GK301"); // The constructor was called 2 times void main( ) { clrscr(); puts("Creation and usage of Group objects\n\n"); puts("Give the names of the GK201 group! " ); //Continued.

GK201.GetGroupNo(groupNoOut ); // Asking for the group number. Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 28./8. t_name nameStr; for ( int k=0; k<GK201.GetCount( ); k++) { printf("Enter the name of the %d th student: ", k); gets( nameStr ); SetElement(k, nameStr); } char groupNoOut [6]; GK201.GetGroupNo(groupNoOut ); // Asking for the group number. printf("Students of the %s group are the next: \n", groupNoOut ); for ( k=0; k<GK201. GetCount(); k++) { printf(" The name of the %d th student: ", k ); GetElement(k, nameStr); puts(nameStr ); } printf("Number of students= %d ", GK201.GetCount( ) ); getch( ); } // The end of main function //Continued.

Department of Information Engineering INFORMATION TECHNOLOGY dr Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 28./9. // Definitions for the constructors and destructor: List :: List( int count0 ) // Definition of the constructor of List class. { count= count0; names = new t_name[ count0 ]; // Dynamic allocation of vector. for (int i = 0; i < count0; i++ ) names[ i ][0]= '\0' ; } List :: ~List( ) // Destructor of List class. { // Freeing of the dynamically allocated names vector. if ( names ) delete[ ] names; } Group :: Group( int count0, char * groupNo0 ) : List( count0 ) { strcpy( groupNo, groupNo0 ); } // The constructor of the base class List can receive default value such a way only, // because the constructor of the List is called and creates the names array first, // then will be called this constructor.

Calling order of constructors Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 28./10. Calling order of constructors // At performing the following object definition, so at creation of object the constructor of // List class will be invoked by the constructor of the Group class passing to it the count0 // parameter. After executing the constructor of the List class the program jumps back to // the block of Group class and the instructions in it will be executed. Group GK201(5, "GK201"); // Object definition, creation. List :: List( int count0 ) // The definition of constructor of the List class. { count= count0; names = new t_name[ count0 ]; // Dynamic vector allocation. for (int i = 0; i < count0; i++ ) names[ i ][0]= '\0' ; } Group :: Group( int count0, char * groupNo0 ): List(count0 ) // Passing the param. { strcpy(groupNo, groupNo0 ); } 1 2

Calling order of destructors Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 28./11. Calling order of destructors // The order of execution of destructors is reversed, fist is executed the destructor of the // object to be destroyed, then, stepping up in the hierarchy, the destructors of the // ancestor classes, one after the another. // The GK201 object will be destroyed at reaching the end of program (in this example). List :: ~List( ) // Destructor of the List class { if ( names ) delete[ ] names; // Freeing the dinamically allocated names vector: } ~Group( ) { }; // This destructor was declared as an automatic inline function // in the class declaration. 2 1

Remarks on example program Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 28./12. Remarks on example program Short member functions defined in the class declaration become inline function automatically without explicit indication of the inline modifier. If the member function definition is given out of class declaration then to connect it to the owner class the scope has to be given: type Class :: MemberFunction(...) { ... }    Using the :: scope operator the ambiguity can be avoided in case of using member functions having the same name and parameter signature in different classes. The own data members and member functions can be referred without scope operator in the own member function definitions. There is a hidden pointer - named this - among parameters of every member function that actually points to that object that was used at function call in the object.Function() format. This pointer can be used directly in the function block making possible to refer to such an address that becomes known after creation of the object only.