SlideShare a Scribd company logo
1 of 29
Download to read offline
CLASSES AND OBJECTS
Prof. K. Adisesha
Learning Outcomes
 Introduction to C++
 Class Definition
 Object Definition
 Access Specifiers
 Member Function
 Defining object of a class
 Accessing member of the class
 Array of Objects
 Objects as function arguments 2
C++ Introduction
C++ Introduction
 C++ is an object-oriented programming language which
allows code to be reused, lowering development costs.
 C++ was developed by Bjarne Stroustrup, as an extension
to the C language.
 C++ gives programmers a high level of control over
system resources and memory.
 The language was updated 3 major times in 2011, 2014,
and 2017 to C++11, C++14, and C++17.
3
Data Types
 Basic Data Types:
 The data type specifies the size and type of information the
variable will store.
4
C++ Operators
 C++ Operators:
 Operators are used to perform operations on variables and
values.
 Example: int x = 100 + 50;
 C++ Operators Types:
 C++ divides the operators into the following groups.
 Arithmetic operators
 Assignment operators
 Comparison operators
 Logical operators
 Bitwise operators
. 5
Arithmetic Operators
 Arithmetic operators are used to perform common
mathematical operations.
6
Assignment Operators
 Assignment operators are used to assign values to
variables.
7
Comparison Operators
 Comparison operators are used to compare two values.
 The return value of a comparison is either true (1) or false (0).
8
Logical Operators
 Logical operators are used to determine the logic between
variables or values.
9
Basic Concepts of OOP’s
 The following are the major characteristics of OOP’s:
 Class
 Objects
 Data abstraction
 Data encapsulation
 Inheritance
 Overloading
 Polymorphism
 Dynamic Binding
 Message Passing
10
Objects
 Objects are basic building blocks for designing programs.
 An object is a collection of data members and associated
member functions.
 An object may represent a person, place or a table of data.
 Each object is identified by a unique name. Each object
must be a member of a particular class.
 Example: Adi, Sunny, Prajwal are the objects of class
PUC.
11
Class
 A class is a collection of objects that have identical
properties, common behavior and shared relationship.
 A class binds the data and its related functions together.
 A class is a user-defined data type that we can use in a
program.
 To create a class, use the class keyword.
 Example:
class MyClass
{
// The class body
};
 Where class keyword is used to create a class called MyClass
12
Class in C++
Definition and Declaration of Classes
 A class definition is a process of naming a class and data
variables, and interface operation of the class.
 The variables declared inside a class are known as data
members.
 The functions declared inside a class are known as member
functions.
 A class declaration specifies the representation of objects of
the class and set of operations that can be applied to such
objects.
13
Class in C++
Definition and Declaration of Classes
 Class body is enclosed in a pair of flower brackets. Class body
contains the declaration of its members (data and functions).
 The functions declared inside a class are known as member
functions.
 The general syntax of the class declaration is:
class User_Defined_Name
{
private :
Data Member;
Member functions;
}; 14
Access Specifiers
Access Specifiers
 Access specifiers define how the members (attributes and
function) of a class can be accessed.
 Every data member of a class is specified by three levels of
access protection for hiding data and function members
internal to the class.
 They help in controlling the access of the data members.
 Different access specifiers are:
 private
 public
 protected
15
Access Specifiers
private:
 private access means a member data can only be accessed by
the class member function or friend function.
 The data members or member functions declared private
cannot be accessed from outside the class.
 The objects of the class can access the private members only
through the public member functions of the class.
 By default data members in a class are private.
 Example:
private:
int x;
float y; 16
Access Specifiers
protected:
 The members which are declared using protected can be
accessed only by the member functions, friend of the class and
also the member functions derived from this class.
 The members cannot be accessed from outside the class.
 The protected access specifier is similar to private access
specifiers.
 Example:
protected:
int x;
float y;
17
Access Specifiers
public:
 public access means that member can be accessed any function
inside or outside the class.
 Some of the public functions of a class provide interface for
accessing the private and protected members of the class.
 Example:
class MyClass
{
public: // Public access specifier
int x; // Public attribute
private: // Private access specifier
int y; // Private attribute
}; 18
Member Function
Member Function:
 Member functions are functions that are included
within a class (Member functions are also called
Methods).
 Member functions can be defined in two places.
 Inside class definition
 Outside class definition
19
Member Function
Inside class definition:
 To define member function inside a class the function declaration
within the class is replaced by actual function definition inside the
class.
 Only small functions are defined inside class definition.
 Example:
20
class rectangle
{
int length, breadth, area;
public:
void get_data( )
{
cout<< ” Enter the values for Length and
Breadth”;
cin>>length>>breadth;
}
void compute( )
{
area = length * breadth;
}
void display( )
{
cout<<” The area of rectangle is”<<area;
}
};
Member Function
Outside class definition:
 To define member function outside the class, the class name
must be linked with the name of member function.
 Scope resolution operator (::) is used to define the member
function outside the class.
 Syntax of a member function defined outside the class is:
return_type class_name : : member_function_name( arg1, ..., argnN)
{
function body;
}
21
Member Function
Program to use member functions inside and outside
class definition:
22
#include<iostream.h>
class item
{
private:
int numbers;
float cost;
public:
void getdata(int a, float b);
void putdata( )
{
cout<<”Number:
“<<number<<endl;
cout<<”Cost:”<<cost<<endl;
}
};
void item : : getdata(int a, float b)
{
number = a;
cost = b;
}
int main( )
{
item x;
x. getdata( 250, 10.5);
x.putdata( );
return 0;
}
Defining object of a class
Object of a class:
 An object is a real world element which is identifiable entity
with some characteristics (attributes) and behavior (functions).
 An object is an instance of a class.
 An object is normally defined in the main ( ) function.
 The syntax for defining objects of a class as follows:
class Class_Name
{
private : //Members
public : //Members
};
class Class_Name Object_name1, Object_name2,……;
 where class keyword is optional.
23
Accessing member of the class
Dot operator (.):
 The public data members of objects of a class can be accessed
using direct member access operator (.).
 Private and protected members of the class can be accessed
only through the member functions of the class.
 No functions outside a class can include statements to access
data directly.
 The syntax of accessing member (data and functions) of a class
is:
a) Syntax for accessing a data member of the class:
Object_Name . data_member;
b) Syntax for accessing a member function of the class:
Object_Name . member_function(arguments); 24
Array of Objects
 An array having class type elements is known as array
of objects.
 An array of objects is declared after definition and is
defined in the same way as any other array.
 Example:
 In the above example, the class employee has two array of
objects contains 15 Lecturers and 2 Admin as objects. 25
class employee
{
private:
char name[10];
int age;
public:
void readdata( );
void displaydata( );
};
employee Lecturer[15];
employee Admin[2];
Objects as function arguments
Function arguments
 A function can receive an object as a function
argument.
 This is similar to any other data being sent as function
argument.
 An object can be passed to a function in two ways:
 Pass by value: Copy of entire object is passed to
function.
 Pass by reference: Only address of the object is
transferred to the function.
26
Objects as function arguments
Pass by Value
 In pass by value, copy of object is passed to the function.
 The function creates its own copy of the object and uses it.
 Therefore changes made to the object inside the function do
not affect the original object.
 Syntax:
void functionName(obj1, obj2)
{ // code to be executed
}
Void main( )
{
functionName(val1, val2)
} 27
Objects as function arguments
Pass by Reference:
 In pass by reference, when an address of an object is passed to
the function, the function directly works on the original object
used in function call.
 This means changes made to the object inside the function will
reflect in the original object, because the function is making
changes in the original object itself.
 Pass by reference is more efficient, since it requires only
passing the address of the object and not the entire object.
 Syntax:
void functionName(&obj1, &obj2)
28
Structure and Classes
Difference between Structure and Classes
Structure Classes
A structure is defined with the struct
keyword
A class is defined with the class
keyword
All the member of a structure are
public by default
All the members of a class are
private by default
Structure cannot be inherit Class can be inherit
A structure contains only data
member
A class contain both data member
and member functions
There is no data hiding features Classes having data hiding features
by using access specifiers(public,
private, protected).
29

More Related Content

What's hot (20)

Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
Object oriented programming c++
Object oriented programming c++Object oriented programming c++
Object oriented programming c++
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
Wrapper class
Wrapper classWrapper class
Wrapper class
 
Constructors and Destructor in C++
Constructors and Destructor in C++Constructors and Destructor in C++
Constructors and Destructor in C++
 
Pointers C programming
Pointers  C programmingPointers  C programming
Pointers C programming
 
07. Virtual Functions
07. Virtual Functions07. Virtual Functions
07. Virtual Functions
 
Templates
TemplatesTemplates
Templates
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
 
C functions
C functionsC functions
C functions
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Friend function & friend class
Friend function & friend classFriend function & friend class
Friend function & friend class
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Control Statements in Java
Control Statements in JavaControl Statements in Java
Control Statements in Java
 
Function in C
Function in CFunction in C
Function in C
 
Class and Objects in Java
Class and Objects in JavaClass and Objects in Java
Class and Objects in Java
 
Characteristics of OOPS
Characteristics of OOPS Characteristics of OOPS
Characteristics of OOPS
 
Functions in python slide share
Functions in python slide shareFunctions in python slide share
Functions in python slide share
 
Constructors in C++
Constructors in C++Constructors in C++
Constructors in C++
 

Similar to Class and object

chapter-7-classes-and-objects.pdf
chapter-7-classes-and-objects.pdfchapter-7-classes-and-objects.pdf
chapter-7-classes-and-objects.pdfstudy material
 
classandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.pptclassandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.pptmanomkpsg
 
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 SCIENCEVenugopalavarma Raja
 
oop lecture 3
oop lecture 3oop lecture 3
oop lecture 3Atif Khan
 
OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++ OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++ Dev Chauhan
 
Presentation on class and object in Object Oriented programming.
Presentation on class and object in Object Oriented programming.Presentation on class and object in Object Oriented programming.
Presentation on class and object in Object Oriented programming.Enam Khan
 
Object and class presentation
Object and class presentationObject and class presentation
Object and class presentationnafisa rahman
 
Classes & objects new
Classes & objects newClasses & objects new
Classes & objects newlykado0dles
 

Similar to Class and object (20)

chapter-7-classes-and-objects.pdf
chapter-7-classes-and-objects.pdfchapter-7-classes-and-objects.pdf
chapter-7-classes-and-objects.pdf
 
Lecture 2 (1)
Lecture 2 (1)Lecture 2 (1)
Lecture 2 (1)
 
C++ Notes
C++ NotesC++ Notes
C++ Notes
 
classandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.pptclassandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.ppt
 
Class and object
Class and objectClass and object
Class and object
 
C++ classes
C++ classesC++ classes
C++ classes
 
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
 
Classes
ClassesClasses
Classes
 
My c++
My c++My c++
My c++
 
4 Classes & Objects
4 Classes & Objects4 Classes & Objects
4 Classes & Objects
 
oop lecture 3
oop lecture 3oop lecture 3
oop lecture 3
 
OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++ OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++
 
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
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Presentation on class and object in Object Oriented programming.
Presentation on class and object in Object Oriented programming.Presentation on class and object in Object Oriented programming.
Presentation on class and object in Object Oriented programming.
 
class c++
class c++class c++
class c++
 
Object and class presentation
Object and class presentationObject and class presentation
Object and class presentation
 
Classes & objects new
Classes & objects newClasses & objects new
Classes & objects new
 
Lecture 4. mte 407
Lecture 4. mte 407Lecture 4. mte 407
Lecture 4. mte 407
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 

More from Prof. Dr. K. Adisesha

Software Engineering notes by K. Adisesha.pdf
Software Engineering notes by K. Adisesha.pdfSoftware Engineering notes by K. Adisesha.pdf
Software Engineering notes by K. Adisesha.pdfProf. Dr. K. Adisesha
 
Software Engineering-Unit 1 by Adisesha.pdf
Software Engineering-Unit 1 by Adisesha.pdfSoftware Engineering-Unit 1 by Adisesha.pdf
Software Engineering-Unit 1 by Adisesha.pdfProf. Dr. K. Adisesha
 
Software Engineering-Unit 2 "Requirement Engineering" by Adi.pdf
Software Engineering-Unit 2 "Requirement Engineering" by Adi.pdfSoftware Engineering-Unit 2 "Requirement Engineering" by Adi.pdf
Software Engineering-Unit 2 "Requirement Engineering" by Adi.pdfProf. Dr. K. Adisesha
 
Software Engineering-Unit 3 "System Modelling" by Adi.pdf
Software Engineering-Unit 3 "System Modelling" by Adi.pdfSoftware Engineering-Unit 3 "System Modelling" by Adi.pdf
Software Engineering-Unit 3 "System Modelling" by Adi.pdfProf. Dr. K. Adisesha
 
Software Engineering-Unit 4 "Architectural Design" by Adi.pdf
Software Engineering-Unit 4 "Architectural Design" by Adi.pdfSoftware Engineering-Unit 4 "Architectural Design" by Adi.pdf
Software Engineering-Unit 4 "Architectural Design" by Adi.pdfProf. Dr. K. Adisesha
 
Software Engineering-Unit 5 "Software Testing"by Adi.pdf
Software Engineering-Unit 5 "Software Testing"by Adi.pdfSoftware Engineering-Unit 5 "Software Testing"by Adi.pdf
Software Engineering-Unit 5 "Software Testing"by Adi.pdfProf. Dr. K. Adisesha
 
Computer Networks Notes by -Dr. K. Adisesha
Computer Networks Notes by -Dr. K. AdiseshaComputer Networks Notes by -Dr. K. Adisesha
Computer Networks Notes by -Dr. K. AdiseshaProf. Dr. K. Adisesha
 
CCN Unit-1&2 Data Communication &Networking by K. Adiaesha
CCN Unit-1&2 Data Communication &Networking by K. AdiaeshaCCN Unit-1&2 Data Communication &Networking by K. Adiaesha
CCN Unit-1&2 Data Communication &Networking by K. AdiaeshaProf. Dr. K. Adisesha
 
CCN Unit-3 Data Link Layer by Dr. K. Adisesha
CCN Unit-3 Data Link Layer by Dr. K. AdiseshaCCN Unit-3 Data Link Layer by Dr. K. Adisesha
CCN Unit-3 Data Link Layer by Dr. K. AdiseshaProf. Dr. K. Adisesha
 
CCN Unit-4 Network Layer by Dr. K. Adisesha
CCN Unit-4 Network Layer by Dr. K. AdiseshaCCN Unit-4 Network Layer by Dr. K. Adisesha
CCN Unit-4 Network Layer by Dr. K. AdiseshaProf. Dr. K. Adisesha
 
CCN Unit-5 Transport & Application Layer by Adi.pdf
CCN Unit-5 Transport & Application Layer by Adi.pdfCCN Unit-5 Transport & Application Layer by Adi.pdf
CCN Unit-5 Transport & Application Layer by Adi.pdfProf. Dr. K. Adisesha
 

More from Prof. Dr. K. Adisesha (20)

Software Engineering notes by K. Adisesha.pdf
Software Engineering notes by K. Adisesha.pdfSoftware Engineering notes by K. Adisesha.pdf
Software Engineering notes by K. Adisesha.pdf
 
Software Engineering-Unit 1 by Adisesha.pdf
Software Engineering-Unit 1 by Adisesha.pdfSoftware Engineering-Unit 1 by Adisesha.pdf
Software Engineering-Unit 1 by Adisesha.pdf
 
Software Engineering-Unit 2 "Requirement Engineering" by Adi.pdf
Software Engineering-Unit 2 "Requirement Engineering" by Adi.pdfSoftware Engineering-Unit 2 "Requirement Engineering" by Adi.pdf
Software Engineering-Unit 2 "Requirement Engineering" by Adi.pdf
 
Software Engineering-Unit 3 "System Modelling" by Adi.pdf
Software Engineering-Unit 3 "System Modelling" by Adi.pdfSoftware Engineering-Unit 3 "System Modelling" by Adi.pdf
Software Engineering-Unit 3 "System Modelling" by Adi.pdf
 
Software Engineering-Unit 4 "Architectural Design" by Adi.pdf
Software Engineering-Unit 4 "Architectural Design" by Adi.pdfSoftware Engineering-Unit 4 "Architectural Design" by Adi.pdf
Software Engineering-Unit 4 "Architectural Design" by Adi.pdf
 
Software Engineering-Unit 5 "Software Testing"by Adi.pdf
Software Engineering-Unit 5 "Software Testing"by Adi.pdfSoftware Engineering-Unit 5 "Software Testing"by Adi.pdf
Software Engineering-Unit 5 "Software Testing"by Adi.pdf
 
Computer Networks Notes by -Dr. K. Adisesha
Computer Networks Notes by -Dr. K. AdiseshaComputer Networks Notes by -Dr. K. Adisesha
Computer Networks Notes by -Dr. K. Adisesha
 
CCN Unit-1&2 Data Communication &Networking by K. Adiaesha
CCN Unit-1&2 Data Communication &Networking by K. AdiaeshaCCN Unit-1&2 Data Communication &Networking by K. Adiaesha
CCN Unit-1&2 Data Communication &Networking by K. Adiaesha
 
CCN Unit-3 Data Link Layer by Dr. K. Adisesha
CCN Unit-3 Data Link Layer by Dr. K. AdiseshaCCN Unit-3 Data Link Layer by Dr. K. Adisesha
CCN Unit-3 Data Link Layer by Dr. K. Adisesha
 
CCN Unit-4 Network Layer by Dr. K. Adisesha
CCN Unit-4 Network Layer by Dr. K. AdiseshaCCN Unit-4 Network Layer by Dr. K. Adisesha
CCN Unit-4 Network Layer by Dr. K. Adisesha
 
CCN Unit-5 Transport & Application Layer by Adi.pdf
CCN Unit-5 Transport & Application Layer by Adi.pdfCCN Unit-5 Transport & Application Layer by Adi.pdf
CCN Unit-5 Transport & Application Layer by Adi.pdf
 
Introduction to Computers.pdf
Introduction to Computers.pdfIntroduction to Computers.pdf
Introduction to Computers.pdf
 
R_Programming.pdf
R_Programming.pdfR_Programming.pdf
R_Programming.pdf
 
Scholarship.pdf
Scholarship.pdfScholarship.pdf
Scholarship.pdf
 
Operating System-2 by Adi.pdf
Operating System-2 by Adi.pdfOperating System-2 by Adi.pdf
Operating System-2 by Adi.pdf
 
Operating System-1 by Adi.pdf
Operating System-1 by Adi.pdfOperating System-1 by Adi.pdf
Operating System-1 by Adi.pdf
 
Operating System-adi.pdf
Operating System-adi.pdfOperating System-adi.pdf
Operating System-adi.pdf
 
Data_structure using C-Adi.pdf
Data_structure using C-Adi.pdfData_structure using C-Adi.pdf
Data_structure using C-Adi.pdf
 
JAVA PPT -2 BY ADI.pdf
JAVA PPT -2 BY ADI.pdfJAVA PPT -2 BY ADI.pdf
JAVA PPT -2 BY ADI.pdf
 
JAVA PPT -5 BY ADI.pdf
JAVA PPT -5 BY ADI.pdfJAVA PPT -5 BY ADI.pdf
JAVA PPT -5 BY ADI.pdf
 

Recently uploaded

Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
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
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxUnboundStockton
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
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
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
MICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxMICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxabhijeetpadhi001
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 

Recently uploaded (20)

Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
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
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
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
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
MICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxMICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptx
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 

Class and object

  • 2. Learning Outcomes  Introduction to C++  Class Definition  Object Definition  Access Specifiers  Member Function  Defining object of a class  Accessing member of the class  Array of Objects  Objects as function arguments 2
  • 3. C++ Introduction C++ Introduction  C++ is an object-oriented programming language which allows code to be reused, lowering development costs.  C++ was developed by Bjarne Stroustrup, as an extension to the C language.  C++ gives programmers a high level of control over system resources and memory.  The language was updated 3 major times in 2011, 2014, and 2017 to C++11, C++14, and C++17. 3
  • 4. Data Types  Basic Data Types:  The data type specifies the size and type of information the variable will store. 4
  • 5. C++ Operators  C++ Operators:  Operators are used to perform operations on variables and values.  Example: int x = 100 + 50;  C++ Operators Types:  C++ divides the operators into the following groups.  Arithmetic operators  Assignment operators  Comparison operators  Logical operators  Bitwise operators . 5
  • 6. Arithmetic Operators  Arithmetic operators are used to perform common mathematical operations. 6
  • 7. Assignment Operators  Assignment operators are used to assign values to variables. 7
  • 8. Comparison Operators  Comparison operators are used to compare two values.  The return value of a comparison is either true (1) or false (0). 8
  • 9. Logical Operators  Logical operators are used to determine the logic between variables or values. 9
  • 10. Basic Concepts of OOP’s  The following are the major characteristics of OOP’s:  Class  Objects  Data abstraction  Data encapsulation  Inheritance  Overloading  Polymorphism  Dynamic Binding  Message Passing 10
  • 11. Objects  Objects are basic building blocks for designing programs.  An object is a collection of data members and associated member functions.  An object may represent a person, place or a table of data.  Each object is identified by a unique name. Each object must be a member of a particular class.  Example: Adi, Sunny, Prajwal are the objects of class PUC. 11
  • 12. Class  A class is a collection of objects that have identical properties, common behavior and shared relationship.  A class binds the data and its related functions together.  A class is a user-defined data type that we can use in a program.  To create a class, use the class keyword.  Example: class MyClass { // The class body };  Where class keyword is used to create a class called MyClass 12
  • 13. Class in C++ Definition and Declaration of Classes  A class definition is a process of naming a class and data variables, and interface operation of the class.  The variables declared inside a class are known as data members.  The functions declared inside a class are known as member functions.  A class declaration specifies the representation of objects of the class and set of operations that can be applied to such objects. 13
  • 14. Class in C++ Definition and Declaration of Classes  Class body is enclosed in a pair of flower brackets. Class body contains the declaration of its members (data and functions).  The functions declared inside a class are known as member functions.  The general syntax of the class declaration is: class User_Defined_Name { private : Data Member; Member functions; }; 14
  • 15. Access Specifiers Access Specifiers  Access specifiers define how the members (attributes and function) of a class can be accessed.  Every data member of a class is specified by three levels of access protection for hiding data and function members internal to the class.  They help in controlling the access of the data members.  Different access specifiers are:  private  public  protected 15
  • 16. Access Specifiers private:  private access means a member data can only be accessed by the class member function or friend function.  The data members or member functions declared private cannot be accessed from outside the class.  The objects of the class can access the private members only through the public member functions of the class.  By default data members in a class are private.  Example: private: int x; float y; 16
  • 17. Access Specifiers protected:  The members which are declared using protected can be accessed only by the member functions, friend of the class and also the member functions derived from this class.  The members cannot be accessed from outside the class.  The protected access specifier is similar to private access specifiers.  Example: protected: int x; float y; 17
  • 18. Access Specifiers public:  public access means that member can be accessed any function inside or outside the class.  Some of the public functions of a class provide interface for accessing the private and protected members of the class.  Example: class MyClass { public: // Public access specifier int x; // Public attribute private: // Private access specifier int y; // Private attribute }; 18
  • 19. Member Function Member Function:  Member functions are functions that are included within a class (Member functions are also called Methods).  Member functions can be defined in two places.  Inside class definition  Outside class definition 19
  • 20. Member Function Inside class definition:  To define member function inside a class the function declaration within the class is replaced by actual function definition inside the class.  Only small functions are defined inside class definition.  Example: 20 class rectangle { int length, breadth, area; public: void get_data( ) { cout<< ” Enter the values for Length and Breadth”; cin>>length>>breadth; } void compute( ) { area = length * breadth; } void display( ) { cout<<” The area of rectangle is”<<area; } };
  • 21. Member Function Outside class definition:  To define member function outside the class, the class name must be linked with the name of member function.  Scope resolution operator (::) is used to define the member function outside the class.  Syntax of a member function defined outside the class is: return_type class_name : : member_function_name( arg1, ..., argnN) { function body; } 21
  • 22. Member Function Program to use member functions inside and outside class definition: 22 #include<iostream.h> class item { private: int numbers; float cost; public: void getdata(int a, float b); void putdata( ) { cout<<”Number: “<<number<<endl; cout<<”Cost:”<<cost<<endl; } }; void item : : getdata(int a, float b) { number = a; cost = b; } int main( ) { item x; x. getdata( 250, 10.5); x.putdata( ); return 0; }
  • 23. Defining object of a class Object of a class:  An object is a real world element which is identifiable entity with some characteristics (attributes) and behavior (functions).  An object is an instance of a class.  An object is normally defined in the main ( ) function.  The syntax for defining objects of a class as follows: class Class_Name { private : //Members public : //Members }; class Class_Name Object_name1, Object_name2,……;  where class keyword is optional. 23
  • 24. Accessing member of the class Dot operator (.):  The public data members of objects of a class can be accessed using direct member access operator (.).  Private and protected members of the class can be accessed only through the member functions of the class.  No functions outside a class can include statements to access data directly.  The syntax of accessing member (data and functions) of a class is: a) Syntax for accessing a data member of the class: Object_Name . data_member; b) Syntax for accessing a member function of the class: Object_Name . member_function(arguments); 24
  • 25. Array of Objects  An array having class type elements is known as array of objects.  An array of objects is declared after definition and is defined in the same way as any other array.  Example:  In the above example, the class employee has two array of objects contains 15 Lecturers and 2 Admin as objects. 25 class employee { private: char name[10]; int age; public: void readdata( ); void displaydata( ); }; employee Lecturer[15]; employee Admin[2];
  • 26. Objects as function arguments Function arguments  A function can receive an object as a function argument.  This is similar to any other data being sent as function argument.  An object can be passed to a function in two ways:  Pass by value: Copy of entire object is passed to function.  Pass by reference: Only address of the object is transferred to the function. 26
  • 27. Objects as function arguments Pass by Value  In pass by value, copy of object is passed to the function.  The function creates its own copy of the object and uses it.  Therefore changes made to the object inside the function do not affect the original object.  Syntax: void functionName(obj1, obj2) { // code to be executed } Void main( ) { functionName(val1, val2) } 27
  • 28. Objects as function arguments Pass by Reference:  In pass by reference, when an address of an object is passed to the function, the function directly works on the original object used in function call.  This means changes made to the object inside the function will reflect in the original object, because the function is making changes in the original object itself.  Pass by reference is more efficient, since it requires only passing the address of the object and not the entire object.  Syntax: void functionName(&obj1, &obj2) 28
  • 29. Structure and Classes Difference between Structure and Classes Structure Classes A structure is defined with the struct keyword A class is defined with the class keyword All the member of a structure are public by default All the members of a class are private by default Structure cannot be inherit Class can be inherit A structure contains only data member A class contain both data member and member functions There is no data hiding features Classes having data hiding features by using access specifiers(public, private, protected). 29