Learning C++ - Class 4

27
CLASS & OOP Ali Aminian & Bardia Nezamzadeh 1

Transcript of Learning C++ - Class 4

Introduction To C++ Programming

CLASS & OOPAli Aminian & Bardia Nezamzadeh1

1

Section OutlineIntroduction to classesConstructors and DestructorsPreceding list of class membersInheritanceMSDNError

2

2

Section OutlineIntroduction to classesConstructors and DestructorsPreceding list of class membersInheritanceMSDNError

3

3

Introduction to ClassesBecause of the need to custom`s type this topic has created, indeed by classes we can define our type and use the effect of thate.g. Car or a particleHow define a custom`s type in C++class MyClass{};4Class members define here that can be any reviewed type up to now

4

Introduction to ClassesIn our class we can define any function or any other type that we need theme.g. class MyCoordinate {int x,y;int object_number;float object_coordinate[100];char back_color;};5

5

Introduction to ClassesUse of classes : an introduction new : if we want to reserve a space in memory Then we use this keyword(indeed if we want to create some type in C++ )MyCoordinateC1 = new MyCoordinate;MyCoordinate* C2 = new MyCoordinate;delete : if we want to destroy a space ( a variable or any thing that reserve a space in memory) we use this keyworddelete C1;delete[] C2;

6

6

Introduction to ClassesUsing :class MyCoordinate {int x,y;int object_number;float object_coordinate[100][2]; char back_color;};void main(){MyCoordinate C1 = new MyCoordinate;MyCoordinate * C2 = new MyCoordinate;return;}

7

7

Introduction to ClassesUsing of a class detail class MyCoordinate {int x,y;int object_number;float object_coordinate[100][2]; char back_color;};void main(){MyCoordinate C1 = new MyCoordinate;MyCoordinate * C2 = new MyCoordinate;C1.object_number = 10;C2->x = 10;return;}

8C1 and C2 are named the Objects of MyCoordinate class

8

Section OutlineIntroduction to classesConstructors and Destructorspreceding list of class membersInheritanceMSDNError

9

9

Constructors and DestructorsIf we want to prepare our class to a special application , constructors help to do this : for example in our class( MyCoordinate ) if we want to set all the object_coordinate by zero we can do this in our constructorNote :constructor can receive parameters for do its responsibility in bestEvery class has a constructor even if there is no programmer`s action about this that by default do not something For define a constructor we introduce a function but there is no output and its name is the same of class name (this is a rule for constructors)

10

10

Constructors and DestructorsNote:In reality whenever a creating object from a special class is done bye new keyword ,the constructor has been called MyCoordinate C = new MyCoordinate(a); its possible that where we need to constructor, call it:: (Scope resolution operator) : by this we can access to the functions in the class. This operator often use in .cpp file and implement of class in its .cppMyCoordinate::MyCordinate()

11From class

11

Constructors and DestructorsWithout Sending parameterclass MyCoordinate {int x,y;int object_number;float object_coordinate[100][2]; char back_color;MyCoordinate(){for (int i=0; i