SlideShare une entreprise Scribd logo
1  sur  32
Using
C++
Object Oriented Programming
Contents
 Features of OOP
 Classes
 Objects & Creating the Objects
 Constructors & Destructors
 Friend Functions & Classes
 Static data members & functions
OOP
 It is programming technique in which programs are written on the
basis of objects
 It is a powerful technique to develop software.
 It is used to analyze and design the application in terms of objects.
 It deals with data and the procedures as a single unit
 Interacting objects handle their own house-keeping.
 Objects in a program interact by sending messages to each other.
 Each object is responsible to initialize and destroy itself.
 There is no need to explicitly call a creation or termination procedure
Features of object-oriented programming
 Data abstraction
the procedure to define a class from objects.
 Encapsulation
A technique for Information Hiding.
 Inheritance
It allows to define a class in terms of another class, which makes it easier
to create and maintain an application.
 Dynamic binding
It determining the method to invoke at runtime instead of at compile time
 Polymorphism
The word polymorphism means having many forms.
Typically, polymorphism occurs when there is a hierarchy of classes and
they are related by inheritance.
Effects of OO methodology on software design
 Maintenance
 Extensibility
 Reusability
Objects
 Object represents an entity in the real world
 Identified by its name
 It consists of two things:
Properties: Characteristics of an object
Functions Actions performed by the object
o Everything is an object
o Systems are composed of objects
 Everything is an object
 A student, a professor
 A desk, a chair, a classroom, a building
 A university, a city, a country
 The world, the universe
 A subject such as CS, IS, Math, History, …
 Systems are composed of objects
 An educational system
 An economic system
 An information system
 A computer system
Design Methodologies
Object-Orientation is a design methodology
 Objects are the building blocks of a program
(interface, editor, menu, file, etc.); data managing
object (db), etc.)
 Objects represent real-world abstractions within an
application.
Properties of Objects
 Characteristics of an object are known as Properties or
attributes of the object
 Each object has its own properties
Example:
 If “Person” is an object, it has following properties
 Name
 Age
 Weight
Object: Car
Properties: Model, Color, Price
Functions of an Object
 Tasks or actions performed by the object are known
as functions or methods.
Classes
 Collection of objects with same
properties and functions
 Use to define characteristics of the
object
 Used as a model for creating different
objects of same type
 Each object of a class is known as an
instance of the class
Declaring a class
 Keyword “class” is used to declare a class
 Declaration specifies:
 Member Variable / Data member
 Function / Member Function
These are common to all objects of that class
Syntax:
class identifier
{
Body of the class
};
Class: is the keyword
Identifier: name of the class to be declared
Access Specifiers
 It specifies the access level of the class members
 Two common access specifiers are:
 Private:
Restrict the use of the class members within the class. It is the default
access specifier. It is used to protect the data members from direct
access from outside the class. Data Member are normally declared with
private access specifier.
 Public
It allows the user to access members within the class as well as outside
the class. It can be accessed from anywhere in the program. Member
functions are normally declared with public access specifier.
Creating objects
 Class is simply a model or prototype for creating objects.
 It is like a new data type that contains both data and
functions.
 Object is created in the same way as other variables are
created.
 Object is also known as instance of a class.
 Process of creating an object is also called instantiation.
Syntax:
class_name object_name;
Class_name: name of the class whose type of object is to be created
Object_name: object to be created.
Executing Member Functions
 Member functions are used to manipulate data
members of a class.
 Member functions can be executed only after
creating objects
Syntax:
Object_name.function();
Object_name: name of object whose member function is to be executed
Function: It is the member function that is need to be executed.
Write a program that
declares a class with a
data member and two
member functions
OUTPUT:
enter number 10
the value of n= 10
Defining member functions outside class
 Function declaration is specified within the class
 Function definition is specified outside the class
 Scope resolution operator :: is used in function declaration if
the function is defined outside the class.
Syntax:
Return_type class_name :: function_name(parameters)
{
function body
}
Return_type type of value to be returned by function
class_name class name to which function belongs
:: scope resoltion operator
function_name name of funtio to be defined
Constructors
 Type of member function that is automatically executed when
an object of that class is created is known as constructor
 It has no return type
 It has same name that of class name
 It work as normal function but cannot return any value
 It is used to initialize data memebrs
Syntax:
name()
{
Constructor body
}
Name: it indicate the name of the constructor
Passing parameters to constructor
 It is same as passing parameters to normal functions
 Only difference is
 Parameters are passed to the constructor when the object is
declared.
Syntax:
type object_name(parameters);
Type: it is the name of the class (type of the object to be declared)
Object_name: name of the object to be declared
Parameter: list of parameters passed to the constructor
Constructor overloading
 Declaring multiple constructors with the same name
but different parameters
 It must differ in one of the following ways
 Number of parameters
 Type of parameter
 Sequence of parameters
Output
the constructor of first=
num = 0
ch = x
the contents of second =
num = 100
ch = p
Default copy constructor
 It is available by default in all classes
 It is used to initialize an object with another object of the
same type.
 User does not need to write this constructor
 It accepts a single object of the same type as parameter.
Syntax:
Class_name object_name(parameter); OR
Class_name object_name = parameter;
Class_name: type of object to be created
Object_name: name of the object
Parameter: name of parameter passed to default constructor
Destructors
 Member function that is automatically executed when an
object of that class is destroyed in known as destructor
 Is has no return type
 Name is same as the class
 It also cannot accept any parameter
 Constructor name proceeded by tilde sign ~
Syntax:
~name()
{
destructor body
}
Objects as function Parameters or Return Type
As parameters:
 Objects can also be passed as parameters to member
functions
 Method is same as passing parameters to other functions
As return type:
 Returning an object from member function is same as
returning a simple variable
 Its return type should be the same as the return type of
the object to be returned.
Static data member
 The type of data member that is shared among all the
objects of the class is known as static data members.
 Defined with static keyword
 If defined static member; only one variable is created in
memory even if there are many objects of that class
 Used to share some data among all objects of a particular
class
 Visible only in the class in which it is defined
 Its lifetime:
 Starts when the program starts its execution
 Ends when the entire program is terminated
Difference between normal and static data members
A
B
N
A
B
A
B
A
B
N
1 2
10
100
10
1
200
20
1
1
200n
Object b1 Object b2 Object b2Object b1
Three normal data members
Two normal data members (a,b) and one
static member (n)
Friend Functions
 Function that is allowed to access the private and protected
members of a particular class from outside the class is
called friend functions
 Friend function of a class
 Not a member function
 Has direct access to private members
Just as member functions do
 Use keyword friend in front of
function declaration
 Specified IN class definition
 But they’re NOT member functions!
Friend Classes
 Entire classes can be friends
 Similar to function being friend to class
 Example:
class F is friend of class C
 All class F member functions are friends of C
 NOT reciprocated
 Friendship granted, not taken
Syntax:
friend class F
 Goes inside class definition of "authorizing" class
Static Function
 A function may be declared with the static keyword
 Static functions live at class level, not at object level
 Static functions may access static variables and
methods, but not dynamic ones
Syntax:
public static int getNumSold(){
return numTicketsSold;
}
class test
{
private:
static int n;
public:
static void show()
{
cout<<“n = “<<n;
}
};
int test::n = 10;
void main()
{
test::show();
getch();
}
Output
n = 10
1 . Write a program that creates three objects of a
class student. Each object of the class must be
assigned a unique roll number.
2. Compare OOP & structured programming
Assignment

Contenu connexe

Tendances

Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
Tech_MX
 

Tendances (20)

FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
 
Data types in c++
Data types in c++Data types in c++
Data types in c++
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Introduction Of C++
Introduction Of C++Introduction Of C++
Introduction Of C++
 
Friend function
Friend functionFriend function
Friend function
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
 
Basic concepts of object oriented programming
Basic concepts of object oriented programmingBasic concepts of object oriented programming
Basic concepts of object oriented programming
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Tokens in C++
Tokens in C++Tokens in C++
Tokens in C++
 
Constructors and Destructor in C++
Constructors and Destructor in C++Constructors and Destructor in C++
Constructors and Destructor in C++
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Type casting in java
Type casting in javaType casting in java
Type casting in java
 
Data types in java
Data types in javaData types in java
Data types in java
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Pure virtual function and abstract class
Pure virtual function and abstract classPure virtual function and abstract class
Pure virtual function and abstract class
 
Datatype in c++ unit 3 -topic 2
Datatype in c++ unit 3 -topic 2Datatype in c++ unit 3 -topic 2
Datatype in c++ unit 3 -topic 2
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions
 
Friend function & friend class
Friend function & friend classFriend function & friend class
Friend function & friend class
 
Java Data Types
Java Data TypesJava Data Types
Java Data Types
 

En vedette

OOP in C - Virtual Function (Chinese Version)
OOP in C - Virtual Function (Chinese Version)OOP in C - Virtual Function (Chinese Version)
OOP in C - Virtual Function (Chinese Version)
Kai-Feng Chou
 
#OOP_D_ITS - 6th - C++ Oop Inheritance
#OOP_D_ITS - 6th - C++ Oop Inheritance#OOP_D_ITS - 6th - C++ Oop Inheritance
#OOP_D_ITS - 6th - C++ Oop Inheritance
Hadziq Fabroyir
 
Dynamics allocation
Dynamics allocationDynamics allocation
Dynamics allocation
Kumar
 
C++ OOP Implementation
C++ OOP ImplementationC++ OOP Implementation
C++ OOP Implementation
Fridz Felisco
 
A COMPLETE FILE FOR C++
A COMPLETE FILE FOR C++A COMPLETE FILE FOR C++
A COMPLETE FILE FOR C++
M Hussnain Ali
 

En vedette (20)

OOP in C++
OOP in C++OOP in C++
OOP in C++
 
C++ Object oriented concepts & programming
C++ Object oriented concepts & programmingC++ Object oriented concepts & programming
C++ Object oriented concepts & programming
 
class c++
class c++class c++
class c++
 
Esoft Metro Campus - Programming with C++
Esoft Metro Campus - Programming with C++Esoft Metro Campus - Programming with C++
Esoft Metro Campus - Programming with C++
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
C++ Programming Language
C++ Programming Language C++ Programming Language
C++ Programming Language
 
Intro. to prog. c++
Intro. to prog. c++Intro. to prog. c++
Intro. to prog. c++
 
Object-oriented concepts
Object-oriented conceptsObject-oriented concepts
Object-oriented concepts
 
Oop l2
Oop l2Oop l2
Oop l2
 
Overview- Skillwise Consulting
Overview- Skillwise Consulting Overview- Skillwise Consulting
Overview- Skillwise Consulting
 
C++ Programming : Learn OOP in C++
C++ Programming : Learn OOP in C++C++ Programming : Learn OOP in C++
C++ Programming : Learn OOP in C++
 
Support for Object-Oriented Programming (OOP) in C++
Support for Object-Oriented Programming (OOP) in C++Support for Object-Oriented Programming (OOP) in C++
Support for Object-Oriented Programming (OOP) in C++
 
OOP in C - Virtual Function (Chinese Version)
OOP in C - Virtual Function (Chinese Version)OOP in C - Virtual Function (Chinese Version)
OOP in C - Virtual Function (Chinese Version)
 
#OOP_D_ITS - 6th - C++ Oop Inheritance
#OOP_D_ITS - 6th - C++ Oop Inheritance#OOP_D_ITS - 6th - C++ Oop Inheritance
#OOP_D_ITS - 6th - C++ Oop Inheritance
 
Intro To C++ - Class 06 - Introduction To Classes, Objects, & Strings, Part II
Intro To C++ - Class 06 - Introduction To Classes, Objects, & Strings, Part IIIntro To C++ - Class 06 - Introduction To Classes, Objects, & Strings, Part II
Intro To C++ - Class 06 - Introduction To Classes, Objects, & Strings, Part II
 
Dynamics allocation
Dynamics allocationDynamics allocation
Dynamics allocation
 
C++ OOP Implementation
C++ OOP ImplementationC++ OOP Implementation
C++ OOP Implementation
 
Control structures in c++
Control structures in c++Control structures in c++
Control structures in c++
 
Pipe & its wall thickness calculation
Pipe & its wall thickness calculationPipe & its wall thickness calculation
Pipe & its wall thickness calculation
 
A COMPLETE FILE FOR C++
A COMPLETE FILE FOR C++A COMPLETE FILE FOR C++
A COMPLETE FILE FOR C++
 

Similaire à Object Oriented Programming Using C++

oops-123991513147-phpapp02.pdf
oops-123991513147-phpapp02.pdfoops-123991513147-phpapp02.pdf
oops-123991513147-phpapp02.pdf
ArpitaJana28
 
Classes, objects and methods
Classes, objects and methodsClasses, objects and methods
Classes, objects and methods
farhan amjad
 
Classes & objects new
Classes & objects newClasses & objects new
Classes & objects new
lykado0dles
 

Similaire à Object Oriented Programming Using C++ (20)

Oops
OopsOops
Oops
 
Class and object
Class and objectClass and object
Class and object
 
classandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.pptclassandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.ppt
 
My c++
My c++My c++
My c++
 
Oops
OopsOops
Oops
 
Class and object
Class and objectClass and object
Class and object
 
oops-123991513147-phpapp02.pdf
oops-123991513147-phpapp02.pdfoops-123991513147-phpapp02.pdf
oops-123991513147-phpapp02.pdf
 
Object Oriented Programming In .Net
Object Oriented Programming In .NetObject Oriented Programming In .Net
Object Oriented Programming In .Net
 
Classes, objects and methods
Classes, objects and methodsClasses, objects and methods
Classes, objects and methods
 
oopusingc.pptx
oopusingc.pptxoopusingc.pptx
oopusingc.pptx
 
OOPs & C++ UNIT 3
OOPs & C++ UNIT 3OOPs & C++ UNIT 3
OOPs & C++ UNIT 3
 
oop lecture 3
oop lecture 3oop lecture 3
oop lecture 3
 
Lab 4 (1).pdf
Lab 4 (1).pdfLab 4 (1).pdf
Lab 4 (1).pdf
 
Lecture 4. mte 407
Lecture 4. mte 407Lecture 4. mte 407
Lecture 4. mte 407
 
Classes & objects new
Classes & objects newClasses & objects new
Classes & objects new
 
Python-Classes.pptx
Python-Classes.pptxPython-Classes.pptx
Python-Classes.pptx
 
oopm 2.pdf
oopm 2.pdfoopm 2.pdf
oopm 2.pdf
 
Learn C# Programming - Classes & Inheritance
Learn C# Programming - Classes & InheritanceLearn C# Programming - Classes & Inheritance
Learn C# Programming - Classes & Inheritance
 
Classes objects in java
Classes objects in javaClasses objects in java
Classes objects in java
 
Objects and Types C#
Objects and Types C#Objects and Types C#
Objects and Types C#
 

Plus de Muhammad Waqas

Plus de Muhammad Waqas (9)

Image processing (Signal Processing)
Image processing (Signal Processing)Image processing (Signal Processing)
Image processing (Signal Processing)
 
Windows 98 vs Windows 200
Windows 98 vs Windows 200Windows 98 vs Windows 200
Windows 98 vs Windows 200
 
Introduction to CPU registers
Introduction to CPU registersIntroduction to CPU registers
Introduction to CPU registers
 
Six exercises to relieve back pain at your workplace
Six exercises to relieve back pain at your workplaceSix exercises to relieve back pain at your workplace
Six exercises to relieve back pain at your workplace
 
Engineering Numerical Analysis Lecture-1
Engineering Numerical Analysis Lecture-1Engineering Numerical Analysis Lecture-1
Engineering Numerical Analysis Lecture-1
 
Chapter7 Computer Networks
Chapter7 Computer NetworksChapter7 Computer Networks
Chapter7 Computer Networks
 
Chapter5 Storage
Chapter5 StorageChapter5 Storage
Chapter5 Storage
 
Chapter4 Data Processing
Chapter4 Data ProcessingChapter4 Data Processing
Chapter4 Data Processing
 
Chapter1 introduction to computer systems
Chapter1 introduction to computer systemsChapter1 introduction to computer systems
Chapter1 introduction to computer systems
 

Dernier

Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 

Dernier (20)

Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 

Object Oriented Programming Using C++

  • 2. Contents  Features of OOP  Classes  Objects & Creating the Objects  Constructors & Destructors  Friend Functions & Classes  Static data members & functions
  • 3. OOP  It is programming technique in which programs are written on the basis of objects  It is a powerful technique to develop software.  It is used to analyze and design the application in terms of objects.  It deals with data and the procedures as a single unit  Interacting objects handle their own house-keeping.  Objects in a program interact by sending messages to each other.  Each object is responsible to initialize and destroy itself.  There is no need to explicitly call a creation or termination procedure
  • 4. Features of object-oriented programming  Data abstraction the procedure to define a class from objects.  Encapsulation A technique for Information Hiding.  Inheritance It allows to define a class in terms of another class, which makes it easier to create and maintain an application.  Dynamic binding It determining the method to invoke at runtime instead of at compile time  Polymorphism The word polymorphism means having many forms. Typically, polymorphism occurs when there is a hierarchy of classes and they are related by inheritance.
  • 5. Effects of OO methodology on software design  Maintenance  Extensibility  Reusability
  • 6. Objects  Object represents an entity in the real world  Identified by its name  It consists of two things: Properties: Characteristics of an object Functions Actions performed by the object o Everything is an object o Systems are composed of objects
  • 7.  Everything is an object  A student, a professor  A desk, a chair, a classroom, a building  A university, a city, a country  The world, the universe  A subject such as CS, IS, Math, History, …  Systems are composed of objects  An educational system  An economic system  An information system  A computer system
  • 8. Design Methodologies Object-Orientation is a design methodology  Objects are the building blocks of a program (interface, editor, menu, file, etc.); data managing object (db), etc.)  Objects represent real-world abstractions within an application.
  • 9. Properties of Objects  Characteristics of an object are known as Properties or attributes of the object  Each object has its own properties Example:  If “Person” is an object, it has following properties  Name  Age  Weight Object: Car Properties: Model, Color, Price
  • 10. Functions of an Object  Tasks or actions performed by the object are known as functions or methods.
  • 11. Classes  Collection of objects with same properties and functions  Use to define characteristics of the object  Used as a model for creating different objects of same type  Each object of a class is known as an instance of the class
  • 12. Declaring a class  Keyword “class” is used to declare a class  Declaration specifies:  Member Variable / Data member  Function / Member Function These are common to all objects of that class Syntax: class identifier { Body of the class }; Class: is the keyword Identifier: name of the class to be declared
  • 13. Access Specifiers  It specifies the access level of the class members  Two common access specifiers are:  Private: Restrict the use of the class members within the class. It is the default access specifier. It is used to protect the data members from direct access from outside the class. Data Member are normally declared with private access specifier.  Public It allows the user to access members within the class as well as outside the class. It can be accessed from anywhere in the program. Member functions are normally declared with public access specifier.
  • 14. Creating objects  Class is simply a model or prototype for creating objects.  It is like a new data type that contains both data and functions.  Object is created in the same way as other variables are created.  Object is also known as instance of a class.  Process of creating an object is also called instantiation. Syntax: class_name object_name; Class_name: name of the class whose type of object is to be created Object_name: object to be created.
  • 15. Executing Member Functions  Member functions are used to manipulate data members of a class.  Member functions can be executed only after creating objects Syntax: Object_name.function(); Object_name: name of object whose member function is to be executed Function: It is the member function that is need to be executed.
  • 16. Write a program that declares a class with a data member and two member functions OUTPUT: enter number 10 the value of n= 10
  • 17. Defining member functions outside class  Function declaration is specified within the class  Function definition is specified outside the class  Scope resolution operator :: is used in function declaration if the function is defined outside the class. Syntax: Return_type class_name :: function_name(parameters) { function body } Return_type type of value to be returned by function class_name class name to which function belongs :: scope resoltion operator function_name name of funtio to be defined
  • 18. Constructors  Type of member function that is automatically executed when an object of that class is created is known as constructor  It has no return type  It has same name that of class name  It work as normal function but cannot return any value  It is used to initialize data memebrs Syntax: name() { Constructor body } Name: it indicate the name of the constructor
  • 19. Passing parameters to constructor  It is same as passing parameters to normal functions  Only difference is  Parameters are passed to the constructor when the object is declared. Syntax: type object_name(parameters); Type: it is the name of the class (type of the object to be declared) Object_name: name of the object to be declared Parameter: list of parameters passed to the constructor
  • 20. Constructor overloading  Declaring multiple constructors with the same name but different parameters  It must differ in one of the following ways  Number of parameters  Type of parameter  Sequence of parameters
  • 21. Output the constructor of first= num = 0 ch = x the contents of second = num = 100 ch = p
  • 22. Default copy constructor  It is available by default in all classes  It is used to initialize an object with another object of the same type.  User does not need to write this constructor  It accepts a single object of the same type as parameter. Syntax: Class_name object_name(parameter); OR Class_name object_name = parameter; Class_name: type of object to be created Object_name: name of the object Parameter: name of parameter passed to default constructor
  • 23. Destructors  Member function that is automatically executed when an object of that class is destroyed in known as destructor  Is has no return type  Name is same as the class  It also cannot accept any parameter  Constructor name proceeded by tilde sign ~ Syntax: ~name() { destructor body }
  • 24. Objects as function Parameters or Return Type As parameters:  Objects can also be passed as parameters to member functions  Method is same as passing parameters to other functions As return type:  Returning an object from member function is same as returning a simple variable  Its return type should be the same as the return type of the object to be returned.
  • 25. Static data member  The type of data member that is shared among all the objects of the class is known as static data members.  Defined with static keyword  If defined static member; only one variable is created in memory even if there are many objects of that class  Used to share some data among all objects of a particular class  Visible only in the class in which it is defined  Its lifetime:  Starts when the program starts its execution  Ends when the entire program is terminated
  • 26.
  • 27. Difference between normal and static data members A B N A B A B A B N 1 2 10 100 10 1 200 20 1 1 200n Object b1 Object b2 Object b2Object b1 Three normal data members Two normal data members (a,b) and one static member (n)
  • 28. Friend Functions  Function that is allowed to access the private and protected members of a particular class from outside the class is called friend functions  Friend function of a class  Not a member function  Has direct access to private members Just as member functions do  Use keyword friend in front of function declaration  Specified IN class definition  But they’re NOT member functions!
  • 29. Friend Classes  Entire classes can be friends  Similar to function being friend to class  Example: class F is friend of class C  All class F member functions are friends of C  NOT reciprocated  Friendship granted, not taken Syntax: friend class F  Goes inside class definition of "authorizing" class
  • 30. Static Function  A function may be declared with the static keyword  Static functions live at class level, not at object level  Static functions may access static variables and methods, but not dynamic ones Syntax: public static int getNumSold(){ return numTicketsSold; }
  • 31. class test { private: static int n; public: static void show() { cout<<“n = “<<n; } }; int test::n = 10; void main() { test::show(); getch(); } Output n = 10
  • 32. 1 . Write a program that creates three objects of a class student. Each object of the class must be assigned a unique roll number. 2. Compare OOP & structured programming Assignment