SlideShare a Scribd company logo
OOP3
Object concept
OOP concepts
When you approach a programming problem in an
object oriented language, you no longer ask how
the problem will be divided into functions but how
it will be divided into objects:
Thinking in terms of objects rather than functions has
helpful effect on how easily you can design
programs, because the real world consists of
objects and there is a close match between objects
in the programming sense and objects in the real
world
Ct’d
What is object?
Many real-world objects have both attribute (xtics that can
change) abilities or responsibilities- (things they can do)
Real-world object = Attributes(state) + Abilities(behavior,
responsibility)
Programming object = Data + Functions
The match between programming object and real-world
objects is the result of combining data and member
functions
How is object defined in C++ program?
Classes and Objects
• Class is a new data type which is used to define objects. A
class serves as a plan , or a template. It specifies what data
and what functions will be included in objects of that class.
Writing a class does not create any objects.
• A class is a description of similar objects
• Objects are instances of classes
• Example: A model (class) to define points in a graphics
program.
Points on a plane must have two properties (states):
– X and y coordinates. we can use two integer variables to
represent these properties.
Ct’d
in our program, points should have the following abilities
(responsibilities):
• Point can move on the plane : move function
• Point can show their coordinates on the screen: print
function
• Point can answer the question whether they are on the
zero point (00) or not: is_zero function
Example point class:
Class point{ // Declaration of point class
Int x,y; // attribute: x and y coordinates .. Attributes
Public: // we will discuss later
void move(int, int); // A function to move the points ..
Behavior
void print(); // to print the coordinates on the screen ..
responsibilities
Bool is _zero(0; //is the point on the zero point (00)
}; // End of class declaration (; must be there)
In our example first data and then the function prototype s
are written . It is also possible to write them in reverse
order.
Data and function s in a class are called members of the class
In our example only the prototypes of the functions are
written in the class declaration. The bodies may take place
in other parts (in other files) of the program.
If the body of a function is written in the class declaration.
Then this function is defined as an inline function(macro).
// ****Bodies of Member Functions****
// a function to move the points
void point::move(int new_x,int new_y)
{
X=new_x; // assigns new value to x coorrdinate
y = new_y; // assigns new value to y coordinate
}
// To print the coordinate on the screen
Void point::print()
{
Cout<<“X= “ <<x<<“,Y= “ <<y << endl;
}
Ct’d
// is the point on the zero point(00)
Bool point::is_zero()
{
return (x ==0) &&(y ==0); // if x=0 AND y=0 returns true
}
Now we have a model (template) to define point objects. We
can create necessary point (objects) using the model.
int main()
{
Point.point1, point2; //2 object are defined: point1 and point2
Point1.move(100,50); // point1 moves to (100,50)
Point1.print(); // point1 coordinate to the screen
Point1.move(20,65); // point1moves to (20,65)
poit1,.print(); // point1’s coordinates on the screen
If (piont1.is_zero()) // is point1 on (00)?
cout <<“point1 is now on zero point(00)” << endl;
else
cout << “ point1 is NOT on zero point(00)” << endl;
point2.move(00); // point2 moves to (00)
if (point2.is_zero()) // is point2 on (00)?
cout << “point2 is now on zero point(00)” << endl;
else
cout <<“ point2 is NOT on zero point (00) << endl;
return o;
}
C++ Terminology
• A class is a grouping of data and functions. A class is very
much like a structure type as used in ANSI-C, it is only a
pattern (a template) to be used to create a variables which
can be manipulated in a program
Classes are design to give certain services
• An object is an instance of a class, which is similar to a
variable defined as an instance of a type. An object is what
you actually use in a program
• An attribute is a data member of a class that can take
different values for different instances (objects) of this
class. Example; Name of a student, coordinates of a point
Ct’d
• A methods (member function) is a function contained
within the class . You will find the functions used within a
class often referred to as methods in programming
literature
classes fulfill their services (responsibilities) by the help of
their methods.
• A message is the same thing as a function call. In object
oriented programming , we send messages instead of
calling functions. For the time being , you can think of
them as identical. Later we will see that they are in fact
slightly different
messages are sent to object to get some services from them.
conclusion
until now that we have discovered some features of the oop
and the c++.
Our programs consist of object as the real world do.
Classes are living (active) data types which are used to
defined objects. We can send messages (orders) to objects
to enable them to do something .
Classes include both data and the functions involved with
these data (encapsulation).As the result:
• Software object are similar to the real world objects
• Programs are easy to read and understand.
• It is easy to find errors
• It supports modularity and teamwork
Defining object as inline Functions
(macro)
In the previous example, only the prototypes of the member
functions are written in the class declaration. The bodies of
the methods are defined outside the class.
It is also possible to write bodies of methods in the class. Such
methods are defined as inline functions
For example the is_zero method of the point class can be
define as an inline function as follows:
class point { //Declaration of point class
int x,y; // properties: x and y coordinates
public:
void move(int, int); // to print the coordinates on the
screen
Ct’d
void print(); // to print the coordinates on the screen
bool is_zero() // is the point on the zero point(0,0) inline
function
{
return (x ==0) &&(y == 0); // the body of is _zero
}
};
NB: Do not write long methods in the class declaration. It
decreases the readability and the performance of the
program
Defining Dynamic Objects
Classes can be used to define variable like built-in data type
(int, float,char etc) of the compiler.
For example it is possible to define pointers to objects. In the
example below two pointers (ptr1 and ptr2) to objects of
type point are defined.
Int main()
{
Point*ptr1 = new point; // allocating memory for the
object pointed by ptr1
Point*ptr2 = new point; // allocating memory for the
object pointed by ptr2
Ct’d
ptr1->move(50,50); //’move’ message to the object
pointed by ptr1
Ptr1->print(); // ‘print’ message to the object pointed
by ptr1
Ptr2->move(100,150); // ‘move’ message to the object
pointed by ptr2
If (ptr2->is_zer0()) // is the object pointed by ptr2 on zero
cout<<“Object pointed by ptr2 is NOT on zero.”<<endl;
delete ptr1 // Releasing the memory
delete ptr2;
return 0;
}
Defining array of Objects
We may define static and dynamic array of objects. In the next
example below we shall see later how to define dynamic array of
objects
int main()
{
Point array[10]; //defining an array with ten objects
Array[0].move(15,40); // ‘move message to the first element
(indices 0)
Array[1].move(75,35); //’move’ message to the second
element (indices 1)
:
for (int i =0; i < 10; i ++) // ‘print’ message to all objects in the array
Ct’d
array[i].print();
return 0;
}
Controlling Access to Members
we can divide programmers into two groups: classs creators (those
who create new data type) and client programmers (the class
consumers who use the data types in their applications)
The goal of the class creator is to build a class that includes all necessary
to the client programmer and keeps everything else hidden
The goal of thee client programmer is to collect a toolbox full of classes
to use for rapid application development
The first reason for access control is to keep client programmer’s hands
off portions they shouldn’t touch. The hidden parts are only
necessary for the internal machinations of the data type but not part
of the interface that users need in order to solve their particular
problems
Ct’d
the second reason for access control is that. If it’s hidden, the
client programmer can’t use it , which means that the class
creator can change the hidden portion at will without
worrying a bout the impact to anyone else.
This protection also prevent accidentally changes of states of
objects.
The label public:, private : (and protected: as well
shall see in future) are use to control access to a
class’ data members and functions
• Private class members can be access only by
member of that class.
Ct’d
Public members may be access by any function in the
program
The default access mode for classes is private: After each
label, the mode that was invoked by that label applies until
the next label or until the end of class declaration.
The primary purpose of public members is to present to the
class’s clients a view of the services the class provides. This
set of services forms the publics interface of the class
The private members are not accessible to the
clients of a class. They form the implementation of
the class
x
y
Void
move
(int, int )
Void
print
Bool
is_zero
Point.move(100,45)
Interface
Public members
If(point.is_zero())
Implementation
Private members
Point1.print()
messages
*
example
• We modify the move function of the class point clients of
this class can not move a point outside a window with a
size of 500x300.
class point { //point class
int x,y; // private members: x and y coordinates
public: // public members
bool move(int,int); //A function to move the points
void print(); // to print the coordinates on the screen
bool is_zero(); // is the point on the zero point (0,0)
};
Ct’d
// a function to move thepoint (0,500x0,300)
Bool point:: move (int new_x, int new_y)
{
if (new_x>0 && new_x< 500 && // if new_x is in 0-500
new_y > 0 && new_y <300) // if new_y is in 0-300
{
x =new_x; // assigns new value to x coordinate
y = new_y; // assigns new value to y coordinate
return true; // input values are accepted
}
return false; // input values are not accepted
}
Ct’d
The new move function returns a boolean value to inform
the client programmer whether the input values are
accepted or not.
Here is the main fnction:
Int main()
{
point p1; // p1 object is defined
int x’y; // Two variables to read some values from
the keyboard
Cout << “Give x and y coordinates”;
cin>>x>>y; // read two values from the keyboard
Ct’d
if (p1.move(x,y) // send move message and check the result
p1.print(); // if result is OK print coordinates on the screen
else
cout <<“nInput values are not accepted”;
}
it is not possible to assign a value to x or y directly outside the class
p1.x = -10; // ERROR! X is private
Struct keyword in c++:
Class and struct keywords have very similar meaning in the c++. They
both are used to build object models. The only difference is their
default access mode
The default access mode for class is private and
The default access mode for struct is public
Friend Functions and Friend Classes
A function or an entire class may be declared to be a friend of
another class.
A friend of a class has the right to access all members
(private, protected or public) of the class.
Class A{
friend class B; // class B is a friend of class A
Private: // public members of A
Int I;
float f;
public: // public members of A
void func1(char*c);
};
Ct’d
Class B{ // class B
int j;
public:
void
func2(A&s){cout<<s.i;} // B can access private members of A
};
Int main() {
A ObjA;
BobjB;
objB.function2(objA);
return 0;
NB here A is NOT a friend of B. A can not access private members of B
Ct’d
A friend function has right to access all members (private,
protected or public) of the class
class point { // point class
friend void zero (point &); // A friend function of point
int x,y; // private members: x, y coordinates
public: // public members
bool move(int, int); // A function to move the points
void print() // to print the coordinates on the screen
bol is_zero(); // is the point on the zero point(0,0)
},
Ct’d
// Sssigns zero to all coordinates
Void zero(print &p) // Not a member of any class
{
p.x = 0; // assign zero to x of p
p.y = 0; // assign zero to y of p
}
This pointer
Each object has its own data space in the memory of the
computer. When an object is defined, memory is allocated
only for its data members.
The code of member functions are created only once. Each
object of the same class uses the same function code.
point1
point2
X = 100
Y = 50
X =200
Y = 300
move
print
Is_zero
Ct’d
How does c++ ensure that the proper object is
referenced?
C++ compiler maintains a pointer , called the this
pointer
A c++ compiler defines an object pointer this . When a
member function is called, this pointer contains the
address of the object, for which the function is
involked.
so member functions can access the data members
using the pointer this.

More Related Content

Similar to Major terminologies in oop (Lect 2).ppt. Object oriented programming (20)

C questions
C questionsC questions
C questions
parm112
 
oop lecture 3
oop lecture 3oop lecture 3
oop lecture 3
Atif Khan
 
C++ questions And Answer
C++ questions And AnswerC++ questions And Answer
C++ questions And Answer
lavparmar007
 
C++ Interview Question And Answer
C++ Interview Question And AnswerC++ Interview Question And Answer
C++ Interview Question And Answer
Jagan Mohan Bishoyi
 
OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++ OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++
Dev Chauhan
 
Interoduction to c++
Interoduction to c++Interoduction to c++
Interoduction to c++
Amresh Raj
 
oopm 2.pdf
oopm 2.pdfoopm 2.pdf
oopm 2.pdf
jayeshsoni49
 
My c++
My c++My c++
My c++
snathick
 
Object Oriented Programming Lecture Notes
Object Oriented Programming Lecture NotesObject Oriented Programming Lecture Notes
Object Oriented Programming Lecture Notes
FellowBuddy.com
 
C++ largest no between three nos
C++ largest no between three nosC++ largest no between three nos
C++ largest no between three nos
krismishra
 
Unit 5.ppt
Unit 5.pptUnit 5.ppt
Unit 5.ppt
JITTAYASHWANTHREDDY
 
Classes and objects1
Classes and objects1Classes and objects1
Classes and objects1
Vineeta Garg
 
New microsoft office word document (2)
New microsoft office word document (2)New microsoft office word document (2)
New microsoft office word document (2)
rashmita_mishra
 
DS Unit 6.ppt
DS Unit 6.pptDS Unit 6.ppt
DS Unit 6.ppt
JITTAYASHWANTHREDDY
 
1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answers1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answers
Akash Gawali
 
Ch.1 oop introduction, classes and objects
Ch.1 oop introduction, classes and objectsCh.1 oop introduction, classes and objects
Ch.1 oop introduction, classes and objects
ITNet
 
Introduction to C++ Programming
Introduction to C++ ProgrammingIntroduction to C++ Programming
Introduction to C++ Programming
Preeti Kashyap
 
Mca 504 dotnet_unit3
Mca 504 dotnet_unit3Mca 504 dotnet_unit3
Mca 504 dotnet_unit3
Rai Saheb Bhanwar Singh College Nasrullaganj
 
Lecture 9
Lecture 9Lecture 9
Lecture 9
Mohammed Saleh
 
3 functions and class
3   functions and class3   functions and class
3 functions and class
trixiacruz
 
C questions
C questionsC questions
C questions
parm112
 
oop lecture 3
oop lecture 3oop lecture 3
oop lecture 3
Atif Khan
 
C++ questions And Answer
C++ questions And AnswerC++ questions And Answer
C++ questions And Answer
lavparmar007
 
C++ Interview Question And Answer
C++ Interview Question And AnswerC++ Interview Question And Answer
C++ Interview Question And Answer
Jagan Mohan Bishoyi
 
OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++ OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++
Dev Chauhan
 
Interoduction to c++
Interoduction to c++Interoduction to c++
Interoduction to c++
Amresh Raj
 
Object Oriented Programming Lecture Notes
Object Oriented Programming Lecture NotesObject Oriented Programming Lecture Notes
Object Oriented Programming Lecture Notes
FellowBuddy.com
 
C++ largest no between three nos
C++ largest no between three nosC++ largest no between three nos
C++ largest no between three nos
krismishra
 
Classes and objects1
Classes and objects1Classes and objects1
Classes and objects1
Vineeta Garg
 
New microsoft office word document (2)
New microsoft office word document (2)New microsoft office word document (2)
New microsoft office word document (2)
rashmita_mishra
 
1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answers1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answers
Akash Gawali
 
Ch.1 oop introduction, classes and objects
Ch.1 oop introduction, classes and objectsCh.1 oop introduction, classes and objects
Ch.1 oop introduction, classes and objects
ITNet
 
Introduction to C++ Programming
Introduction to C++ ProgrammingIntroduction to C++ Programming
Introduction to C++ Programming
Preeti Kashyap
 
3 functions and class
3   functions and class3   functions and class
3 functions and class
trixiacruz
 

Recently uploaded (20)

Search Engine Optimization (SEO) for Website Success
Search Engine Optimization (SEO) for Website SuccessSearch Engine Optimization (SEO) for Website Success
Search Engine Optimization (SEO) for Website Success
Muneeb Rana
 
Swachata Quiz - Prelims - 01.10.24 - Quiz Club IIT Patna
Swachata Quiz - Prelims - 01.10.24 - Quiz Club IIT PatnaSwachata Quiz - Prelims - 01.10.24 - Quiz Club IIT Patna
Swachata Quiz - Prelims - 01.10.24 - Quiz Club IIT Patna
Quiz Club, Indian Institute of Technology, Patna
 
Smart Borrowing: Everything You Need to Know About Short Term Loans in India
Smart Borrowing: Everything You Need to Know About Short Term Loans in IndiaSmart Borrowing: Everything You Need to Know About Short Term Loans in India
Smart Borrowing: Everything You Need to Know About Short Term Loans in India
fincrifcontent
 
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Analysis of Quantitative Data Parametric and non-parametric tests.pptxAnalysis of Quantitative Data Parametric and non-parametric tests.pptx
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Shrutidhara2
 
How to Create Time Off Request in Odoo 18 Time Off
How to Create Time Off Request in Odoo 18 Time OffHow to Create Time Off Request in Odoo 18 Time Off
How to Create Time Off Request in Odoo 18 Time Off
Celine George
 
Pragya Champion's Chalice 2025 Set , General Quiz
Pragya Champion's Chalice 2025 Set , General QuizPragya Champion's Chalice 2025 Set , General Quiz
Pragya Champion's Chalice 2025 Set , General Quiz
Pragya - UEM Kolkata Quiz Club
 
la storia dell'Inghilterra, letteratura inglese
la storia dell'Inghilterra, letteratura inglesela storia dell'Inghilterra, letteratura inglese
la storia dell'Inghilterra, letteratura inglese
LetiziaLucente
 
Fatman Book HD Pdf by aayush songare.pdf
Fatman Book  HD Pdf by aayush songare.pdfFatman Book  HD Pdf by aayush songare.pdf
Fatman Book HD Pdf by aayush songare.pdf
Aayush Songare
 
LDMMIA Bonus GUEST GRAD Student Check-in
LDMMIA Bonus GUEST GRAD Student Check-inLDMMIA Bonus GUEST GRAD Student Check-in
LDMMIA Bonus GUEST GRAD Student Check-in
LDM & Mia eStudios
 
Optimization technique in pharmaceutical product development.pptx
Optimization technique in pharmaceutical product development.pptxOptimization technique in pharmaceutical product development.pptx
Optimization technique in pharmaceutical product development.pptx
UrmiPrajapati3
 
Uterine Prolapse, causes type and classification,its managment
Uterine Prolapse, causes type and classification,its managmentUterine Prolapse, causes type and classification,its managment
Uterine Prolapse, causes type and classification,its managment
Ritu480198
 
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
GeorgeDiamandis11
 
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptxSEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
PoojaSen20
 
Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...
Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...
Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...
EduSkills OECD
 
Semisolid_Dosage_Forms.pptx
Semisolid_Dosage_Forms.pptxSemisolid_Dosage_Forms.pptx
Semisolid_Dosage_Forms.pptx
Shantanu Ranjan
 
How to Manage Maintenance Request in Odoo 18
How to Manage Maintenance Request in Odoo 18How to Manage Maintenance Request in Odoo 18
How to Manage Maintenance Request in Odoo 18
Celine George
 
THE CHURCH AND ITS IMPACT: FOSTERING CHRISTIAN EDUCATION
THE CHURCH AND ITS IMPACT: FOSTERING CHRISTIAN EDUCATIONTHE CHURCH AND ITS IMPACT: FOSTERING CHRISTIAN EDUCATION
THE CHURCH AND ITS IMPACT: FOSTERING CHRISTIAN EDUCATION
PROF. PAUL ALLIEU KAMARA
 
Hemiptera & Neuroptera: Insect Diversity.pptx
Hemiptera & Neuroptera: Insect Diversity.pptxHemiptera & Neuroptera: Insect Diversity.pptx
Hemiptera & Neuroptera: Insect Diversity.pptx
Arshad Shaikh
 
POS Reporting in Odoo 18 - Odoo 18 Slides
POS Reporting in Odoo 18 - Odoo 18 SlidesPOS Reporting in Odoo 18 - Odoo 18 Slides
POS Reporting in Odoo 18 - Odoo 18 Slides
Celine George
 
প্রত্যুৎপন্নমতিত্ব - Prottutponnomotittwa 2025.pdf
প্রত্যুৎপন্নমতিত্ব - Prottutponnomotittwa 2025.pdfপ্রত্যুৎপন্নমতিত্ব - Prottutponnomotittwa 2025.pdf
প্রত্যুৎপন্নমতিত্ব - Prottutponnomotittwa 2025.pdf
Pragya - UEM Kolkata Quiz Club
 
Search Engine Optimization (SEO) for Website Success
Search Engine Optimization (SEO) for Website SuccessSearch Engine Optimization (SEO) for Website Success
Search Engine Optimization (SEO) for Website Success
Muneeb Rana
 
Smart Borrowing: Everything You Need to Know About Short Term Loans in India
Smart Borrowing: Everything You Need to Know About Short Term Loans in IndiaSmart Borrowing: Everything You Need to Know About Short Term Loans in India
Smart Borrowing: Everything You Need to Know About Short Term Loans in India
fincrifcontent
 
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Analysis of Quantitative Data Parametric and non-parametric tests.pptxAnalysis of Quantitative Data Parametric and non-parametric tests.pptx
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Shrutidhara2
 
How to Create Time Off Request in Odoo 18 Time Off
How to Create Time Off Request in Odoo 18 Time OffHow to Create Time Off Request in Odoo 18 Time Off
How to Create Time Off Request in Odoo 18 Time Off
Celine George
 
la storia dell'Inghilterra, letteratura inglese
la storia dell'Inghilterra, letteratura inglesela storia dell'Inghilterra, letteratura inglese
la storia dell'Inghilterra, letteratura inglese
LetiziaLucente
 
Fatman Book HD Pdf by aayush songare.pdf
Fatman Book  HD Pdf by aayush songare.pdfFatman Book  HD Pdf by aayush songare.pdf
Fatman Book HD Pdf by aayush songare.pdf
Aayush Songare
 
LDMMIA Bonus GUEST GRAD Student Check-in
LDMMIA Bonus GUEST GRAD Student Check-inLDMMIA Bonus GUEST GRAD Student Check-in
LDMMIA Bonus GUEST GRAD Student Check-in
LDM & Mia eStudios
 
Optimization technique in pharmaceutical product development.pptx
Optimization technique in pharmaceutical product development.pptxOptimization technique in pharmaceutical product development.pptx
Optimization technique in pharmaceutical product development.pptx
UrmiPrajapati3
 
Uterine Prolapse, causes type and classification,its managment
Uterine Prolapse, causes type and classification,its managmentUterine Prolapse, causes type and classification,its managment
Uterine Prolapse, causes type and classification,its managment
Ritu480198
 
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
GeorgeDiamandis11
 
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptxSEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
PoojaSen20
 
Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...
Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...
Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...
EduSkills OECD
 
Semisolid_Dosage_Forms.pptx
Semisolid_Dosage_Forms.pptxSemisolid_Dosage_Forms.pptx
Semisolid_Dosage_Forms.pptx
Shantanu Ranjan
 
How to Manage Maintenance Request in Odoo 18
How to Manage Maintenance Request in Odoo 18How to Manage Maintenance Request in Odoo 18
How to Manage Maintenance Request in Odoo 18
Celine George
 
THE CHURCH AND ITS IMPACT: FOSTERING CHRISTIAN EDUCATION
THE CHURCH AND ITS IMPACT: FOSTERING CHRISTIAN EDUCATIONTHE CHURCH AND ITS IMPACT: FOSTERING CHRISTIAN EDUCATION
THE CHURCH AND ITS IMPACT: FOSTERING CHRISTIAN EDUCATION
PROF. PAUL ALLIEU KAMARA
 
Hemiptera & Neuroptera: Insect Diversity.pptx
Hemiptera & Neuroptera: Insect Diversity.pptxHemiptera & Neuroptera: Insect Diversity.pptx
Hemiptera & Neuroptera: Insect Diversity.pptx
Arshad Shaikh
 
POS Reporting in Odoo 18 - Odoo 18 Slides
POS Reporting in Odoo 18 - Odoo 18 SlidesPOS Reporting in Odoo 18 - Odoo 18 Slides
POS Reporting in Odoo 18 - Odoo 18 Slides
Celine George
 
প্রত্যুৎপন্নমতিত্ব - Prottutponnomotittwa 2025.pdf
প্রত্যুৎপন্নমতিত্ব - Prottutponnomotittwa 2025.pdfপ্রত্যুৎপন্নমতিত্ব - Prottutponnomotittwa 2025.pdf
প্রত্যুৎপন্নমতিত্ব - Prottutponnomotittwa 2025.pdf
Pragya - UEM Kolkata Quiz Club
 
Ad

Major terminologies in oop (Lect 2).ppt. Object oriented programming

  • 2. OOP concepts When you approach a programming problem in an object oriented language, you no longer ask how the problem will be divided into functions but how it will be divided into objects: Thinking in terms of objects rather than functions has helpful effect on how easily you can design programs, because the real world consists of objects and there is a close match between objects in the programming sense and objects in the real world
  • 3. Ct’d What is object? Many real-world objects have both attribute (xtics that can change) abilities or responsibilities- (things they can do) Real-world object = Attributes(state) + Abilities(behavior, responsibility) Programming object = Data + Functions The match between programming object and real-world objects is the result of combining data and member functions How is object defined in C++ program?
  • 4. Classes and Objects • Class is a new data type which is used to define objects. A class serves as a plan , or a template. It specifies what data and what functions will be included in objects of that class. Writing a class does not create any objects. • A class is a description of similar objects • Objects are instances of classes • Example: A model (class) to define points in a graphics program. Points on a plane must have two properties (states): – X and y coordinates. we can use two integer variables to represent these properties.
  • 5. Ct’d in our program, points should have the following abilities (responsibilities): • Point can move on the plane : move function • Point can show their coordinates on the screen: print function • Point can answer the question whether they are on the zero point (00) or not: is_zero function
  • 6. Example point class: Class point{ // Declaration of point class Int x,y; // attribute: x and y coordinates .. Attributes Public: // we will discuss later void move(int, int); // A function to move the points .. Behavior void print(); // to print the coordinates on the screen .. responsibilities Bool is _zero(0; //is the point on the zero point (00) }; // End of class declaration (; must be there)
  • 7. In our example first data and then the function prototype s are written . It is also possible to write them in reverse order. Data and function s in a class are called members of the class In our example only the prototypes of the functions are written in the class declaration. The bodies may take place in other parts (in other files) of the program. If the body of a function is written in the class declaration. Then this function is defined as an inline function(macro).
  • 8. // ****Bodies of Member Functions**** // a function to move the points void point::move(int new_x,int new_y) { X=new_x; // assigns new value to x coorrdinate y = new_y; // assigns new value to y coordinate } // To print the coordinate on the screen Void point::print() { Cout<<“X= “ <<x<<“,Y= “ <<y << endl; }
  • 9. Ct’d // is the point on the zero point(00) Bool point::is_zero() { return (x ==0) &&(y ==0); // if x=0 AND y=0 returns true }
  • 10. Now we have a model (template) to define point objects. We can create necessary point (objects) using the model. int main() { Point.point1, point2; //2 object are defined: point1 and point2 Point1.move(100,50); // point1 moves to (100,50) Point1.print(); // point1 coordinate to the screen Point1.move(20,65); // point1moves to (20,65) poit1,.print(); // point1’s coordinates on the screen If (piont1.is_zero()) // is point1 on (00)? cout <<“point1 is now on zero point(00)” << endl;
  • 11. else cout << “ point1 is NOT on zero point(00)” << endl; point2.move(00); // point2 moves to (00) if (point2.is_zero()) // is point2 on (00)? cout << “point2 is now on zero point(00)” << endl; else cout <<“ point2 is NOT on zero point (00) << endl; return o; }
  • 12. C++ Terminology • A class is a grouping of data and functions. A class is very much like a structure type as used in ANSI-C, it is only a pattern (a template) to be used to create a variables which can be manipulated in a program Classes are design to give certain services • An object is an instance of a class, which is similar to a variable defined as an instance of a type. An object is what you actually use in a program • An attribute is a data member of a class that can take different values for different instances (objects) of this class. Example; Name of a student, coordinates of a point
  • 13. Ct’d • A methods (member function) is a function contained within the class . You will find the functions used within a class often referred to as methods in programming literature classes fulfill their services (responsibilities) by the help of their methods. • A message is the same thing as a function call. In object oriented programming , we send messages instead of calling functions. For the time being , you can think of them as identical. Later we will see that they are in fact slightly different messages are sent to object to get some services from them.
  • 14. conclusion until now that we have discovered some features of the oop and the c++. Our programs consist of object as the real world do. Classes are living (active) data types which are used to defined objects. We can send messages (orders) to objects to enable them to do something . Classes include both data and the functions involved with these data (encapsulation).As the result: • Software object are similar to the real world objects • Programs are easy to read and understand. • It is easy to find errors • It supports modularity and teamwork
  • 15. Defining object as inline Functions (macro) In the previous example, only the prototypes of the member functions are written in the class declaration. The bodies of the methods are defined outside the class. It is also possible to write bodies of methods in the class. Such methods are defined as inline functions For example the is_zero method of the point class can be define as an inline function as follows: class point { //Declaration of point class int x,y; // properties: x and y coordinates public: void move(int, int); // to print the coordinates on the screen
  • 16. Ct’d void print(); // to print the coordinates on the screen bool is_zero() // is the point on the zero point(0,0) inline function { return (x ==0) &&(y == 0); // the body of is _zero } }; NB: Do not write long methods in the class declaration. It decreases the readability and the performance of the program
  • 17. Defining Dynamic Objects Classes can be used to define variable like built-in data type (int, float,char etc) of the compiler. For example it is possible to define pointers to objects. In the example below two pointers (ptr1 and ptr2) to objects of type point are defined. Int main() { Point*ptr1 = new point; // allocating memory for the object pointed by ptr1 Point*ptr2 = new point; // allocating memory for the object pointed by ptr2
  • 18. Ct’d ptr1->move(50,50); //’move’ message to the object pointed by ptr1 Ptr1->print(); // ‘print’ message to the object pointed by ptr1 Ptr2->move(100,150); // ‘move’ message to the object pointed by ptr2 If (ptr2->is_zer0()) // is the object pointed by ptr2 on zero cout<<“Object pointed by ptr2 is NOT on zero.”<<endl; delete ptr1 // Releasing the memory delete ptr2; return 0; }
  • 19. Defining array of Objects We may define static and dynamic array of objects. In the next example below we shall see later how to define dynamic array of objects int main() { Point array[10]; //defining an array with ten objects Array[0].move(15,40); // ‘move message to the first element (indices 0) Array[1].move(75,35); //’move’ message to the second element (indices 1) : for (int i =0; i < 10; i ++) // ‘print’ message to all objects in the array
  • 21. Controlling Access to Members we can divide programmers into two groups: classs creators (those who create new data type) and client programmers (the class consumers who use the data types in their applications) The goal of the class creator is to build a class that includes all necessary to the client programmer and keeps everything else hidden The goal of thee client programmer is to collect a toolbox full of classes to use for rapid application development The first reason for access control is to keep client programmer’s hands off portions they shouldn’t touch. The hidden parts are only necessary for the internal machinations of the data type but not part of the interface that users need in order to solve their particular problems
  • 22. Ct’d the second reason for access control is that. If it’s hidden, the client programmer can’t use it , which means that the class creator can change the hidden portion at will without worrying a bout the impact to anyone else. This protection also prevent accidentally changes of states of objects. The label public:, private : (and protected: as well shall see in future) are use to control access to a class’ data members and functions • Private class members can be access only by member of that class.
  • 23. Ct’d Public members may be access by any function in the program The default access mode for classes is private: After each label, the mode that was invoked by that label applies until the next label or until the end of class declaration. The primary purpose of public members is to present to the class’s clients a view of the services the class provides. This set of services forms the publics interface of the class The private members are not accessible to the clients of a class. They form the implementation of the class
  • 24. x y Void move (int, int ) Void print Bool is_zero Point.move(100,45) Interface Public members If(point.is_zero()) Implementation Private members Point1.print() messages *
  • 25. example • We modify the move function of the class point clients of this class can not move a point outside a window with a size of 500x300. class point { //point class int x,y; // private members: x and y coordinates public: // public members bool move(int,int); //A function to move the points void print(); // to print the coordinates on the screen bool is_zero(); // is the point on the zero point (0,0) };
  • 26. Ct’d // a function to move thepoint (0,500x0,300) Bool point:: move (int new_x, int new_y) { if (new_x>0 && new_x< 500 && // if new_x is in 0-500 new_y > 0 && new_y <300) // if new_y is in 0-300 { x =new_x; // assigns new value to x coordinate y = new_y; // assigns new value to y coordinate return true; // input values are accepted } return false; // input values are not accepted }
  • 27. Ct’d The new move function returns a boolean value to inform the client programmer whether the input values are accepted or not. Here is the main fnction: Int main() { point p1; // p1 object is defined int x’y; // Two variables to read some values from the keyboard Cout << “Give x and y coordinates”; cin>>x>>y; // read two values from the keyboard
  • 28. Ct’d if (p1.move(x,y) // send move message and check the result p1.print(); // if result is OK print coordinates on the screen else cout <<“nInput values are not accepted”; } it is not possible to assign a value to x or y directly outside the class p1.x = -10; // ERROR! X is private Struct keyword in c++: Class and struct keywords have very similar meaning in the c++. They both are used to build object models. The only difference is their default access mode The default access mode for class is private and The default access mode for struct is public
  • 29. Friend Functions and Friend Classes A function or an entire class may be declared to be a friend of another class. A friend of a class has the right to access all members (private, protected or public) of the class. Class A{ friend class B; // class B is a friend of class A Private: // public members of A Int I; float f; public: // public members of A void func1(char*c); };
  • 30. Ct’d Class B{ // class B int j; public: void func2(A&s){cout<<s.i;} // B can access private members of A }; Int main() { A ObjA; BobjB; objB.function2(objA); return 0; NB here A is NOT a friend of B. A can not access private members of B
  • 31. Ct’d A friend function has right to access all members (private, protected or public) of the class class point { // point class friend void zero (point &); // A friend function of point int x,y; // private members: x, y coordinates public: // public members bool move(int, int); // A function to move the points void print() // to print the coordinates on the screen bol is_zero(); // is the point on the zero point(0,0) },
  • 32. Ct’d // Sssigns zero to all coordinates Void zero(print &p) // Not a member of any class { p.x = 0; // assign zero to x of p p.y = 0; // assign zero to y of p }
  • 33. This pointer Each object has its own data space in the memory of the computer. When an object is defined, memory is allocated only for its data members. The code of member functions are created only once. Each object of the same class uses the same function code. point1 point2 X = 100 Y = 50 X =200 Y = 300 move print Is_zero
  • 34. Ct’d How does c++ ensure that the proper object is referenced? C++ compiler maintains a pointer , called the this pointer A c++ compiler defines an object pointer this . When a member function is called, this pointer contains the address of the object, for which the function is involked. so member functions can access the data members using the pointer this.

Editor's Notes