SlideShare a Scribd company logo
1 of 23
UNIT 1
LECTURE 2LECTURE 2
Title : Object Oriented Programming
Subject Code : 3XT03
Semester : Third
Department : Electronics & Telecommunication Engineering
Prof. Avinash S. Kapse
Contents
Introduction to classes and object.
Declaration of class and objects.
Structure of C++ class.
Prof. Avinash S. Kapse
Last Lecture Review
In last lecture, we have seen following points:
Structure of
C++ program
Variable
s in C++
Input and Output
Statements
Prof. Avinash S. Kapse
Objectives
After completing this lecture, you will
come to know and understand the
following points:
After completing this lecture, you will
come to know and understand the
following points:
Classes and ObjectClasses and Object
Structure of
C++ class
Structure of
C++ class
Access Specifier
Scope Resolution
Operator
Prof. Avinash S. Kapse
Classes and Object
A class is an expanded concept of a data structure: instead
of holding only data, it can hold both data and functions.
An object is an instantiation of a class. In terms of
variables, a class would be the type, and an object would be the
variable.
Classes are generally declared using the keyword class,
with the format which is given on next slide:
Prof. Avinash S. Kapse
Cont…
class class_name
{
access_specifier_1:
member1;
access_specifier_2:
member2;
...
} object_names;
class_name:- This is a valid identifier for the class.
object_names:- This is an optional list of names for objects of this
class.
Prof. Avinash S. Kapse
Cont…
The body of the declaration can contain members, that can
be either data or function declarations, and optionally access
specifiers.
Access specifiers modify the access rights that the members
following them acquire:
• private
• protected
• public
Prof. Avinash S. Kapse
Cont…
private members of a class are accessible only from within
other members of the same class or from their friends.
protected members are accessible from members of their
same class and from their friends, but also from members of their
derived classes.
public members are accessible from anywhere where the
object is visible.
Prof. Avinash S. Kapse
Cont…
By default, all members of a class declared with the class
keyword have private access for all its members. Therefore, any
member that is declared before one other class specifier
automatically has private access. For example:
class CRectangle
{
int x, y;
public:
void set_values (int,int);
int area (void);
} rect;
Prof. Avinash S. Kapse
Cont…
Declares a class (i.e., a type) called CRectangle and an
object (i.e., a variable) of this class called rect. This class contains
four members: two data members of type int (member x and
member y) with private access (because private is the default
access level) and two member functions with public access:
set_values() and area(), of which for now we have only included
their declaration, not their definition.
Prof. Avinash S. Kapse
Objects
1. An object is an instance of class. An object is a thing like
vehicle, employee or anything.
2. An object consist of data and operations that manipulate those
data i.e. methods.
3. Syntax:
class_name object1, object2……..objectN;
where class_name is name of the class to which object belong and
object1, object2…..objectN are objects.
Prof. Avinash S. Kapse
Example
// classes example
#include <iostream.h>
class Crectangle
{
int x, y;
public:
void set_values (int,int);
int area () {return (x*y);}
};
Header File
Class Declaration
Access Specifier
Function declaration
Prof. Avinash S. Kapse
Example 1
void Crectangle :: set_values (int a, int b)
{
x = a;
y = b;
}
main () {
CRectangle rect;
rect.set_values (3,4);
cout << "area: " << rect.area();
}
Function definition
with two parameters a
and b
Object rect has been
created of class
CRectangle
Prof. Avinash S. Kapse
Explanation
The most important new thing in this code is the operator of
scope (::, two colons) included in the definition of set_values(). It is used
to define a member of a class from outside the class definition itself.
You may notice that the definition of the member function area()
has been included directly within the definition of the CRectangle class.
Note:- When we want to declare a function inside a class and definition
outside the class then use :: (Scope Resolution Operator).
Prof. Avinash S. Kapse
Cont…
1. Members x and y have private access.
2. By declaring them private we deny access to them from
anywhere outside the class.
3. CRectangle rect; This statement is used to create object rect of
class Crectangle.
4. rect.set_values (3,4); This statement is used to call a function
i.e. set_values(3,4). 3 is assigned to a and 4 is assigned to b
where a and b are parameters of function set_values().
Prof. Avinash S. Kapse
Example 2
#include <iostream.h>
class MyClass
{
int a,b;
void getData(int x, int y)
{
a=x;
b=y;
}
Function definition
with two parameters x
and y
Prof. Avinash S. Kapse
Cont…
void putData( )
{
cout<<“ a = ” <<a<<endl;
cout<<“ b = ” <<b<<endl;
}
};
void main () {
MyClass m1,m2;
m1.getData(5,10);
This function will
display the value of a
and b
Two objects have been
created i.e. m1 and m2
getData( ) function is
called with object m1.
Prof. Avinash S. Kapse
Cont…
m1.putData();
m2.getData(15,100);
m2.putData();
}
Output of the above program:
a = 5
b = 10
a = 15
b= 100
putData( ) function is
called with object m1.
getData( ) and
putData( ) function is
called with object m2.
Prof. Avinash S. Kapse
Example 3
WAP to accept and print student Roll Number and Name of a Student.
#include <iostream.h>
class MyStudent
{
int rollno;
char name[25];
void getData()
{
cout<<“Enter Roll Number and Name of a Student”<<endl;
cin>>rollno>>name;
}
Prof. Avinash S. Kapse
Cont…
void putData()
{
cout<<“ Roll Number : ” <<rollno<<endl;
cout<<“ Name of Student : ” <<name<<endl;
}
};
void main () {
MyStudent s1;
s1.getData();
s1.putData();
}
Prof. Avinash S. Kapse
Cont…
Output of the above program:
Enter Roll Number and Name of a Student
36 Bajirao Singham
Roll Number : 36
Name of Student: Bajirao Singham
Prof. Avinash S. Kapse
Exercise
1. WAP to read five numbers from user and calculate average.
2. WAP to calculate simple interest.
3. WAP to calculate percentage of a student by reading marks of
five subjects and maximum marks for each subject is 100.
Prof. Avinash S. Kapse
Home Work
1. What is object oriented programming?
2. What is polymorphism OOP?
3. What is Encapsulation OOP?
4. What is Inheritance OOP? Explain their types.
Prof. Avinash S. Kapse

More Related Content

What's hot

Class & Object - Intro
Class & Object - IntroClass & Object - Intro
Class & Object - IntroPRN USM
 
constructor with default arguments and dynamic initialization of objects
constructor with default arguments and dynamic initialization of objectsconstructor with default arguments and dynamic initialization of objects
constructor with default arguments and dynamic initialization of objectsKanhaiya Saxena
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++HalaiHansaika
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and DestructorKamal Acharya
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructorsNilesh Dalvi
 
Java Foundations: Basic Syntax, Conditions, Loops
Java Foundations: Basic Syntax, Conditions, LoopsJava Foundations: Basic Syntax, Conditions, Loops
Java Foundations: Basic Syntax, Conditions, LoopsSvetlin Nakov
 
Functions, classes & objects in c++
Functions, classes & objects in c++Functions, classes & objects in c++
Functions, classes & objects in c++ThamizhselviKrishnam
 
Chapter 13 introduction to classes
Chapter 13 introduction to classesChapter 13 introduction to classes
Chapter 13 introduction to classesrsnyder3601
 
Chapter 6.6
Chapter 6.6Chapter 6.6
Chapter 6.6sotlsoc
 
Class & Object - User Defined Method
Class & Object - User Defined MethodClass & Object - User Defined Method
Class & Object - User Defined MethodPRN USM
 
constructor & destructor in cpp
constructor & destructor in cppconstructor & destructor in cpp
constructor & destructor in cppgourav kottawar
 
Java assignment 1
Java assignment 1Java assignment 1
Java assignment 1Daman Toor
 
SPF Getting Started - Console Program
SPF Getting Started - Console ProgramSPF Getting Started - Console Program
SPF Getting Started - Console ProgramHock Leng PUAH
 

What's hot (19)

Class & Object - Intro
Class & Object - IntroClass & Object - Intro
Class & Object - Intro
 
Inheritance
InheritanceInheritance
Inheritance
 
constructor with default arguments and dynamic initialization of objects
constructor with default arguments and dynamic initialization of objectsconstructor with default arguments and dynamic initialization of objects
constructor with default arguments and dynamic initialization of objects
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
Java Foundations: Basic Syntax, Conditions, Loops
Java Foundations: Basic Syntax, Conditions, LoopsJava Foundations: Basic Syntax, Conditions, Loops
Java Foundations: Basic Syntax, Conditions, Loops
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
 
Functions, classes & objects in c++
Functions, classes & objects in c++Functions, classes & objects in c++
Functions, classes & objects in c++
 
Chapter 13 introduction to classes
Chapter 13 introduction to classesChapter 13 introduction to classes
Chapter 13 introduction to classes
 
Chapter 6.6
Chapter 6.6Chapter 6.6
Chapter 6.6
 
ccc
cccccc
ccc
 
Class & Object - User Defined Method
Class & Object - User Defined MethodClass & Object - User Defined Method
Class & Object - User Defined Method
 
14 Defining Classes
14 Defining Classes14 Defining Classes
14 Defining Classes
 
constructor & destructor in cpp
constructor & destructor in cppconstructor & destructor in cpp
constructor & destructor in cpp
 
Java assignment 1
Java assignment 1Java assignment 1
Java assignment 1
 
SPF Getting Started - Console Program
SPF Getting Started - Console ProgramSPF Getting Started - Console Program
SPF Getting Started - Console Program
 
C++ tutorials
C++ tutorialsC++ tutorials
C++ tutorials
 
Introduction to c ++ part -2
Introduction to c ++   part -2Introduction to c ++   part -2
Introduction to c ++ part -2
 

Similar to Lecture 2

Similar to Lecture 2 (20)

3 functions and class
3   functions and class3   functions and class
3 functions and class
 
chapter-7-classes-and-objects.pdf
chapter-7-classes-and-objects.pdfchapter-7-classes-and-objects.pdf
chapter-7-classes-and-objects.pdf
 
Class and object in C++ By Pawan Thakur
Class and object in C++ By Pawan ThakurClass and object in C++ By Pawan Thakur
Class and object in C++ By Pawan Thakur
 
C++ Notes
C++ NotesC++ Notes
C++ Notes
 
Classes & objects new
Classes & objects newClasses & objects new
Classes & objects new
 
Class and object
Class and objectClass and object
Class and object
 
Lecture 2 (1)
Lecture 2 (1)Lecture 2 (1)
Lecture 2 (1)
 
OOPs & C++ UNIT 3
OOPs & C++ UNIT 3OOPs & C++ UNIT 3
OOPs & C++ UNIT 3
 
Class and object
Class and objectClass and object
Class and object
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
Module IV_updated(old).pdf
Module IV_updated(old).pdfModule IV_updated(old).pdf
Module IV_updated(old).pdf
 
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCECLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
 
OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++ OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++
 
Java class
Java classJava class
Java class
 
Object and class
Object and classObject and class
Object and class
 
Class and object
Class and objectClass and object
Class and object
 
Ppt of c++ vs c#
Ppt of c++ vs c#Ppt of c++ vs c#
Ppt of c++ vs c#
 
object oriented programming language by c++
object oriented programming language by c++object oriented programming language by c++
object oriented programming language by c++
 
Inheritance
InheritanceInheritance
Inheritance
 
Lecture 2, c++(complete reference,herbet sheidt)chapter-12
Lecture 2, c++(complete reference,herbet sheidt)chapter-12Lecture 2, c++(complete reference,herbet sheidt)chapter-12
Lecture 2, c++(complete reference,herbet sheidt)chapter-12
 

More from Avinash Kapse

More from Avinash Kapse (9)

Presentation1
Presentation1Presentation1
Presentation1
 
Presentation1
Presentation1Presentation1
Presentation1
 
SS UII Lecture 1
SS UII Lecture 1SS UII Lecture 1
SS UII Lecture 1
 
SS UI Lecture 5
SS UI Lecture 5SS UI Lecture 5
SS UI Lecture 5
 
SS UI Lecture 6
SS UI Lecture 6SS UI Lecture 6
SS UI Lecture 6
 
SS UI Lecture 4
SS UI Lecture 4SS UI Lecture 4
SS UI Lecture 4
 
SS UI Lecture 1
SS UI Lecture 1SS UI Lecture 1
SS UI Lecture 1
 
Ss ui lecture 2
Ss ui lecture 2Ss ui lecture 2
Ss ui lecture 2
 
Ss ui lecture 1
Ss ui lecture 1Ss ui lecture 1
Ss ui lecture 1
 

Recently uploaded

Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 

Recently uploaded (20)

Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 

Lecture 2

  • 1. UNIT 1 LECTURE 2LECTURE 2 Title : Object Oriented Programming Subject Code : 3XT03 Semester : Third Department : Electronics & Telecommunication Engineering Prof. Avinash S. Kapse
  • 2. Contents Introduction to classes and object. Declaration of class and objects. Structure of C++ class. Prof. Avinash S. Kapse
  • 3. Last Lecture Review In last lecture, we have seen following points: Structure of C++ program Variable s in C++ Input and Output Statements Prof. Avinash S. Kapse
  • 4. Objectives After completing this lecture, you will come to know and understand the following points: After completing this lecture, you will come to know and understand the following points: Classes and ObjectClasses and Object Structure of C++ class Structure of C++ class Access Specifier Scope Resolution Operator Prof. Avinash S. Kapse
  • 5. Classes and Object A class is an expanded concept of a data structure: instead of holding only data, it can hold both data and functions. An object is an instantiation of a class. In terms of variables, a class would be the type, and an object would be the variable. Classes are generally declared using the keyword class, with the format which is given on next slide: Prof. Avinash S. Kapse
  • 6. Cont… class class_name { access_specifier_1: member1; access_specifier_2: member2; ... } object_names; class_name:- This is a valid identifier for the class. object_names:- This is an optional list of names for objects of this class. Prof. Avinash S. Kapse
  • 7. Cont… The body of the declaration can contain members, that can be either data or function declarations, and optionally access specifiers. Access specifiers modify the access rights that the members following them acquire: • private • protected • public Prof. Avinash S. Kapse
  • 8. Cont… private members of a class are accessible only from within other members of the same class or from their friends. protected members are accessible from members of their same class and from their friends, but also from members of their derived classes. public members are accessible from anywhere where the object is visible. Prof. Avinash S. Kapse
  • 9. Cont… By default, all members of a class declared with the class keyword have private access for all its members. Therefore, any member that is declared before one other class specifier automatically has private access. For example: class CRectangle { int x, y; public: void set_values (int,int); int area (void); } rect; Prof. Avinash S. Kapse
  • 10. Cont… Declares a class (i.e., a type) called CRectangle and an object (i.e., a variable) of this class called rect. This class contains four members: two data members of type int (member x and member y) with private access (because private is the default access level) and two member functions with public access: set_values() and area(), of which for now we have only included their declaration, not their definition. Prof. Avinash S. Kapse
  • 11. Objects 1. An object is an instance of class. An object is a thing like vehicle, employee or anything. 2. An object consist of data and operations that manipulate those data i.e. methods. 3. Syntax: class_name object1, object2……..objectN; where class_name is name of the class to which object belong and object1, object2…..objectN are objects. Prof. Avinash S. Kapse
  • 12. Example // classes example #include <iostream.h> class Crectangle { int x, y; public: void set_values (int,int); int area () {return (x*y);} }; Header File Class Declaration Access Specifier Function declaration Prof. Avinash S. Kapse
  • 13. Example 1 void Crectangle :: set_values (int a, int b) { x = a; y = b; } main () { CRectangle rect; rect.set_values (3,4); cout << "area: " << rect.area(); } Function definition with two parameters a and b Object rect has been created of class CRectangle Prof. Avinash S. Kapse
  • 14. Explanation The most important new thing in this code is the operator of scope (::, two colons) included in the definition of set_values(). It is used to define a member of a class from outside the class definition itself. You may notice that the definition of the member function area() has been included directly within the definition of the CRectangle class. Note:- When we want to declare a function inside a class and definition outside the class then use :: (Scope Resolution Operator). Prof. Avinash S. Kapse
  • 15. Cont… 1. Members x and y have private access. 2. By declaring them private we deny access to them from anywhere outside the class. 3. CRectangle rect; This statement is used to create object rect of class Crectangle. 4. rect.set_values (3,4); This statement is used to call a function i.e. set_values(3,4). 3 is assigned to a and 4 is assigned to b where a and b are parameters of function set_values(). Prof. Avinash S. Kapse
  • 16. Example 2 #include <iostream.h> class MyClass { int a,b; void getData(int x, int y) { a=x; b=y; } Function definition with two parameters x and y Prof. Avinash S. Kapse
  • 17. Cont… void putData( ) { cout<<“ a = ” <<a<<endl; cout<<“ b = ” <<b<<endl; } }; void main () { MyClass m1,m2; m1.getData(5,10); This function will display the value of a and b Two objects have been created i.e. m1 and m2 getData( ) function is called with object m1. Prof. Avinash S. Kapse
  • 18. Cont… m1.putData(); m2.getData(15,100); m2.putData(); } Output of the above program: a = 5 b = 10 a = 15 b= 100 putData( ) function is called with object m1. getData( ) and putData( ) function is called with object m2. Prof. Avinash S. Kapse
  • 19. Example 3 WAP to accept and print student Roll Number and Name of a Student. #include <iostream.h> class MyStudent { int rollno; char name[25]; void getData() { cout<<“Enter Roll Number and Name of a Student”<<endl; cin>>rollno>>name; } Prof. Avinash S. Kapse
  • 20. Cont… void putData() { cout<<“ Roll Number : ” <<rollno<<endl; cout<<“ Name of Student : ” <<name<<endl; } }; void main () { MyStudent s1; s1.getData(); s1.putData(); } Prof. Avinash S. Kapse
  • 21. Cont… Output of the above program: Enter Roll Number and Name of a Student 36 Bajirao Singham Roll Number : 36 Name of Student: Bajirao Singham Prof. Avinash S. Kapse
  • 22. Exercise 1. WAP to read five numbers from user and calculate average. 2. WAP to calculate simple interest. 3. WAP to calculate percentage of a student by reading marks of five subjects and maximum marks for each subject is 100. Prof. Avinash S. Kapse
  • 23. Home Work 1. What is object oriented programming? 2. What is polymorphism OOP? 3. What is Encapsulation OOP? 4. What is Inheritance OOP? Explain their types. Prof. Avinash S. Kapse