SlideShare uma empresa Scribd logo
1 de 29
Baixar para ler 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

Mais conteúdo relacionado

Mais procurados

Classes in c++ (OOP Presentation)
Classes in c++ (OOP Presentation)Classes in c++ (OOP Presentation)
Classes in c++ (OOP Presentation)
Majid Saeed
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).ppt
Alok Kumar
 
Inline function
Inline functionInline function
Inline function
Tech_MX
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt
Tareq Hasan
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
Abhilash Nair
 

Mais procurados (20)

Destructors
DestructorsDestructors
Destructors
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Classes in c++ (OOP Presentation)
Classes in c++ (OOP Presentation)Classes in c++ (OOP Presentation)
Classes in c++ (OOP Presentation)
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).ppt
 
Inline function
Inline functionInline function
Inline function
 
virtual function
virtual functionvirtual function
virtual function
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
 
Unary operator overloading
Unary operator overloadingUnary operator overloading
Unary operator overloading
 
Pointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cppPointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cpp
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt
 
07. Virtual Functions
07. Virtual Functions07. Virtual Functions
07. Virtual Functions
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Abstract class in c++
Abstract class in c++Abstract class in c++
Abstract class in c++
 
Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
Operators in C++
Operators in C++Operators in C++
Operators in C++
 
OOPS Basics With Example
OOPS Basics With ExampleOOPS Basics With Example
OOPS Basics With Example
 

Semelhante a Class and object

Classes & objects new
Classes & objects newClasses & objects new
Classes & objects new
lykado0dles
 

Semelhante a 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 Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++
 
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
 

Mais de Prof. Dr. K. Adisesha

Mais de 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
 

Último

Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 

Último (20)

How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
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
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 

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