2. Section Outline
• Introduction to classes
• Constructors and Destructors
• Preceding list of class members
• Inheritance
• MSDN
• Error
2
3. Section Outline
• Introduction to classes
• Constructors and Destructors
• Preceding list of class members
• Inheritance
• MSDN
• Error
3
4. Introduction to Classes
• Because of the need to custom`s type this
topic has created, indeed by classes we can
define our type and use the effect of that
e.g. Car or a particle
• How define a custom`s type in C++
class MyClass
{
………………
………………
};
4
Class members
define here that
can be any
reviewed type
up to now
5. Introduction to Classes
• In our class we can define any function or any
other type that we need them
e.g. class MyCoordinate {
int x,y;
int object_number;
float object_coordinate[100];
char back_color;
};
5
6. Introduction to Classes
• Use of classes : an introduction
i. 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++ )
MyCoordinate C1 = new MyCoordinate;
MyCoordinate* C2 = new MyCoordinate;
ii. delete : if we want to destroy a space ( a variable or
any thing that reserve a space in memory) we use
this keyword
delete C1;
delete[] C2;
6
7. Introduction to Classes
• Using :
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
8. Introduction to Classes
• Using 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;
}
8
C1 and C2 are named the
Objects of MyCoordinate
class
9. Section Outline
• Introduction to classes
• Constructors and Destructors
• preceding list of class members
• Inheritance
• MSDN
• Error
9
10. Constructors and Destructors
• If 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
constructor
• Note :
constructor can receive parameters for do its responsibility in
best
Every 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
11. Constructors and Destructors
• Note:
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 .cpp
MyCoordinate::MyCordinate()
11
From class
12. Constructors and Destructors
• Without Sending parameter
class MyCoordinate {
int x,y;
int object_number;
float object_coordinate[100][2];
char back_color;
MyCoordinate(){
for (int i=0; i<100; i++)
for(int j = 0; j < 100; j++)
object_coordinate[i][j]=0;
}
};
void main(){
MyCoordinate C = new MyCoordinate;
return;
}
12
As we new an object, the
constructor had been called
• Sending parameter
class MyCoordinate {
int x,y;
int object_number;
float object_coordinate[100][2];
char back_color;
MyCoordinate(int a){
for (int i=0; i<100; i++)
for(int j = 0; j < 100; j++)
object_coordinate[i][j]=a;
}
};
void main(){
MyCoordinate C = new MyCoordinate(a);
return;
}
13. Constructors and Destructors
• How to ordering codes in
related to classes by .h and
.cpp files
#include “MyCoordinate.h”
void main() {
MyCoordinate *c1 = new MyCoordinate(1);
cout << c1->x << endl;
cout << c2-> y << endl;
return;
}
13
//MyCoordinate.h
class MyCoordinate {
int x,y;
int object_number;
float bject_coordinate[100][2];
MyCoordinate(int);
char back_color;
void set_xy (int , int );
};
//MyCoordinate.cpp
void MyCoordinate::set_xy(int a, int b){
x=a;y=b;retrun;}
MyCoordinate:: MyCoordinate (int a){
For(int i=0; i<100, i++)
for(int j=0; j<100; j++)
object_coordinate[i][j] =a;
}
14. Constructors and Destructors
• In some cases if we work with pointers, if an
object`s scope has finished, we must destroy
reserved space in memory ourselves, this can
be don bye “Destructors”
• Note :
Versus constructors ,destructors can not receive any
parameter
• For define a destructor we introduce a function but
there is no output and its name is the same of class
name, but before its name there is a “~”(this is a rule
for destructors)
14
15. Constructors and Destructors
15
• Without Sending parameter
class MyCoordinate {
int x,y;
int object_number;
float object_coordinate[100][2];
char back_color;
MyCoordinate(){
for (int i=0; i<100; i++)
for(int j = 0; j < 100; j++)
object_coordinate[i][j]=0;
}
~MyCoordinate(){
delete[] floate;
}
};
void main(){
MyCoordinate C = new MyCoordinate;
return;
}
After this accolade the
destructor has been called
and its commands execute
16. Section Outline
• Introduction to classes
• Constructors and Destructors
• preceding list of class members
• Inheritance
• MSDN
• Error
16
17. preceding list of class members
• Because of safe code and having order in
coding there is some level in accessing to the
class`s members
class MyCoordinate {
public:
// Members can be accessed from any object
private:
// Can only be accessed by MyCoordinate for its own use.
};
17
18. preceding list of class members
• In a better definition of MyCoordinate class
we have :
class MyCoordinate {
private:
int x,y;
int object number;
float object_coordinate[100][2];
char back_color;
public:
MyCoordinate(int);
~MyCoordinate();
};
18
19. preceding list of class members
• Some rule about programming :
– Because of safety programming the public
members must be in minimum
– The C++ compiler treats our members as private
members , if we do not define private or public
– It is usual to use get and set function so that we
can manipulate the private members
19
20. Section Outline
• Introduction to classes
• Constructors and Destructors
• preceding list of class members
• Inheritance
• MSDN
• Error
20
21. Inheritance
• Because of the reality treatment nature of C++
,the objects in C++ can inherits from parents
(like a parent an its child)
• In this inheritance child can inherits some
feature from its parent
class child : parent_class
{
…………..
…………..
};
21
22. Inheritance
• e.g.
class 3D_coordinate : public MyCoordinate{
private :
int z;
};
• Note:3D_coordinate has all the feature of
MyCoordinate class ,it means this class has a x
and y axis and a 2D array
22
23. Section Outline
• Introduction to classes
• Constructors and Destructors
• preceding list of class members
• Inheritance
• MSDN
• Error
23
24. • It is a good help for programming in C++(F1)
void vrb = 23;
MSDN
24
Launch MSDN help
about “void”
25. Section Outline
• Introduction to classes
• Constructors and Destructors
• preceding list of class members
• Inheritance
• MSDN
• Error
25
26. Error
• Syntax
• Exception
e.g. int a = 2;
int b = a/0;
• Checking errors
– Break point
– F10 : execute function as a line(do not show the
function`s body)
– F11 : execute function`s body
26