SlideShare une entreprise Scribd logo
1  sur  27
CLASS & OOP
Ali Aminian & Bardia Nezamzadeh
1
Section Outline
• Introduction to classes
• Constructors and Destructors
• Preceding list of class members
• Inheritance
• MSDN
• Error
2
Section Outline
• Introduction to classes
• Constructors and Destructors
• Preceding list of class members
• Inheritance
• MSDN
• Error
3
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
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
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
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
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
Section Outline
• Introduction to classes
• Constructors and Destructors
• preceding list of class members
• Inheritance
• MSDN
• Error
9
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
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
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;
}
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;
}
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
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
Section Outline
• Introduction to classes
• Constructors and Destructors
• preceding list of class members
• Inheritance
• MSDN
• Error
16
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
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
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
Section Outline
• Introduction to classes
• Constructors and Destructors
• preceding list of class members
• Inheritance
• MSDN
• Error
20
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
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
Section Outline
• Introduction to classes
• Constructors and Destructors
• preceding list of class members
• Inheritance
• MSDN
• Error
23
• It is a good help for programming in C++(F1)
void vrb = 23;
MSDN
24
Launch MSDN help
about “void”
Section Outline
• Introduction to classes
• Constructors and Destructors
• preceding list of class members
• Inheritance
• MSDN
• Error
25
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
finish
27

Contenu connexe

Tendances

Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
Kamal Acharya
 
C++ Multiple Inheritance
C++ Multiple InheritanceC++ Multiple Inheritance
C++ Multiple Inheritance
harshaltambe
 
Csharp4 objects and_types
Csharp4 objects and_typesCsharp4 objects and_types
Csharp4 objects and_types
Abed Bukhari
 
Classes in c++ (OOP Presentation)
Classes in c++ (OOP Presentation)Classes in c++ (OOP Presentation)
Classes in c++ (OOP Presentation)
Majid Saeed
 

Tendances (20)

Constructor and destructor
Constructor and destructorConstructor and destructor
Constructor and destructor
 
Functions, classes & objects in c++
Functions, classes & objects in c++Functions, classes & objects in c++
Functions, classes & objects in c++
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
 
C++ polymorphism
C++ polymorphismC++ polymorphism
C++ polymorphism
 
Constructors & destructors
Constructors & destructorsConstructors & destructors
Constructors & destructors
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
 
C++ Constructor destructor
C++ Constructor destructorC++ Constructor destructor
C++ Constructor destructor
 
Object Oriented Programming - Constructors & Destructors
Object Oriented Programming - Constructors & DestructorsObject Oriented Programming - Constructors & Destructors
Object Oriented Programming - Constructors & Destructors
 
Inheritance, polymorphisam, abstract classes and composition)
Inheritance, polymorphisam, abstract classes and composition)Inheritance, polymorphisam, abstract classes and composition)
Inheritance, polymorphisam, abstract classes and composition)
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of Constructors
 
Constructor and Destructor PPT
Constructor and Destructor PPTConstructor and Destructor PPT
Constructor and Destructor PPT
 
C++ Multiple Inheritance
C++ Multiple InheritanceC++ Multiple Inheritance
C++ Multiple Inheritance
 
Inheritance : Extending Classes
Inheritance : Extending ClassesInheritance : Extending Classes
Inheritance : Extending Classes
 
Constructor and destructor
Constructor  and  destructor Constructor  and  destructor
Constructor and destructor
 
Constructor & Destructor
Constructor & DestructorConstructor & Destructor
Constructor & Destructor
 
Constructor
ConstructorConstructor
Constructor
 
14. Defining Classes
14. Defining Classes14. Defining Classes
14. Defining Classes
 
Csharp4 objects and_types
Csharp4 objects and_typesCsharp4 objects and_types
Csharp4 objects and_types
 
Constructor ppt
Constructor pptConstructor ppt
Constructor ppt
 
Classes in c++ (OOP Presentation)
Classes in c++ (OOP Presentation)Classes in c++ (OOP Presentation)
Classes in c++ (OOP Presentation)
 

En vedette

Education
EducationEducation
Education
IQDA1
 
Project Based Learning
Project Based LearningProject Based Learning
Project Based Learning
smarteach1
 
CV Letícia Abreu (versão não Europass)
CV Letícia Abreu (versão não Europass)CV Letícia Abreu (versão não Europass)
CV Letícia Abreu (versão não Europass)
Let Abreu
 
Ship-to-Shore Solutions
Ship-to-Shore SolutionsShip-to-Shore Solutions
Ship-to-Shore Solutions
Jon Marcy
 

En vedette (19)

2CPP04 - Objects and Classes
2CPP04 - Objects and Classes2CPP04 - Objects and Classes
2CPP04 - Objects and Classes
 
coffee cup
coffee cupcoffee cup
coffee cup
 
Canon advert anaylsis sheet
Canon advert anaylsis sheetCanon advert anaylsis sheet
Canon advert anaylsis sheet
 
Adolfo diaz 2
Adolfo diaz 2Adolfo diaz 2
Adolfo diaz 2
 
Education
EducationEducation
Education
 
TIK BAB 6 KELAS 9
TIK BAB 6 KELAS 9TIK BAB 6 KELAS 9
TIK BAB 6 KELAS 9
 
Engranes y poleas
Engranes y poleasEngranes y poleas
Engranes y poleas
 
Project Based Learning
Project Based LearningProject Based Learning
Project Based Learning
 
Salvadore mondlane amdc asm workshop april 2016
Salvadore mondlane amdc asm workshop april 2016Salvadore mondlane amdc asm workshop april 2016
Salvadore mondlane amdc asm workshop april 2016
 
Cine
CineCine
Cine
 
Evernote Orientation
Evernote OrientationEvernote Orientation
Evernote Orientation
 
Mercedes advert script
Mercedes advert scriptMercedes advert script
Mercedes advert script
 
Voiceover Artist Booking Sheet
Voiceover Artist Booking SheetVoiceover Artist Booking Sheet
Voiceover Artist Booking Sheet
 
CV Letícia Abreu (versão não Europass)
CV Letícia Abreu (versão não Europass)CV Letícia Abreu (versão não Europass)
CV Letícia Abreu (versão não Europass)
 
Radio Production Schedule Call Sheet
Radio Production Schedule Call SheetRadio Production Schedule Call Sheet
Radio Production Schedule Call Sheet
 
Learning C++ - Pointers in c++ 2
Learning C++ - Pointers in c++ 2Learning C++ - Pointers in c++ 2
Learning C++ - Pointers in c++ 2
 
Ship-to-Shore Solutions
Ship-to-Shore SolutionsShip-to-Shore Solutions
Ship-to-Shore Solutions
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
 
Constructor and destructor in c++
Constructor and destructor in c++Constructor and destructor in c++
Constructor and destructor in c++
 

Similaire à Learning C++ - Class 4

Presentation 1st
Presentation 1stPresentation 1st
Presentation 1st
Connex
 

Similaire à Learning C++ - Class 4 (20)

Constructors and destructors in C++ part 2
Constructors and destructors in C++ part 2Constructors and destructors in C++ part 2
Constructors and destructors in C++ part 2
 
Presentation 1st
Presentation 1stPresentation 1st
Presentation 1st
 
Mca 2nd sem u-2 classes & objects
Mca 2nd  sem u-2 classes & objectsMca 2nd  sem u-2 classes & objects
Mca 2nd sem u-2 classes & objects
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
 
Oops concept in c#
Oops concept in c#Oops concept in c#
Oops concept in c#
 
Oops
OopsOops
Oops
 
OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++ OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++
 
Constructor
ConstructorConstructor
Constructor
 
Oops concept on c#
Oops concept on c#Oops concept on c#
Oops concept on c#
 
Constructors & Destructors [Compatibility Mode].pdf
Constructors & Destructors [Compatibility Mode].pdfConstructors & Destructors [Compatibility Mode].pdf
Constructors & Destructors [Compatibility Mode].pdf
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorials
 
UNIT I (1).ppt
UNIT I (1).pptUNIT I (1).ppt
UNIT I (1).ppt
 
UNIT I (1).ppt
UNIT I (1).pptUNIT I (1).ppt
UNIT I (1).ppt
 
Object Oriented Programming using C++ - Part 2
Object Oriented Programming using C++ - Part 2Object Oriented Programming using C++ - Part 2
Object Oriented Programming using C++ - Part 2
 
Constructors and destructors in C++
Constructors and destructors in  C++Constructors and destructors in  C++
Constructors and destructors in C++
 
9781285852744 ppt ch10
9781285852744 ppt ch109781285852744 ppt ch10
9781285852744 ppt ch10
 
9781285852744 ppt ch10
9781285852744 ppt ch109781285852744 ppt ch10
9781285852744 ppt ch10
 
Python_Unit_2 OOPS.pptx
Python_Unit_2  OOPS.pptxPython_Unit_2  OOPS.pptx
Python_Unit_2 OOPS.pptx
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
C++ Classes Tutorials.ppt
C++ Classes Tutorials.pptC++ Classes Tutorials.ppt
C++ Classes Tutorials.ppt
 

Dernier

FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
dollysharma2066
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Dr.Costas Sachpazis
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
rknatarajan
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Christo Ananth
 

Dernier (20)

FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdf
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLPVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
Vivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design SpainVivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design Spain
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...
 

Learning C++ - Class 4

  • 1. CLASS & OOP Ali Aminian & Bardia Nezamzadeh 1
  • 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