SlideShare a Scribd company logo
1 of 73
CLASSES AND OBJECTS
ANIL KUMAR
https://www.facebook.com/AniLK0221
INTRODUCTION
• C++ class mechanism allows users to define
their own data types that can be used as
conveniently as build in types.
• Classes are often called user defined types.
• Includes defining of class types and creation of
objects of classes.
Cont…………
• The class definition introduces both :
• the class data members that define the
internal representation of class
• The class member functions that define the
set of operations that may be applied to
objects of class type.
Cont………….
• Information hiding is achieved by declaring
the data members as private whereas the
operations to be performed on class objects
by the program are public.
What is class?
• Created using keyword class.
• A class declaration defines a new user defined
data type that links code and data.
• The new data type is then used to declare
objects of that class.
• Class is a logical abstraction but an object has
physical existence.
• Objects is an instance of class.
Cont……….
• The class is the cornerstone of C++
– It makes possible encapsulation, data hiding and
inheritance
– A user defined type
– Consists of both data and methods
– Defines properties and behavior of that type
Syntax of Class declaration
• Class className
• {
• // body of a class
• }
Cont…………….
• The class definition has two parts:
• Class head : composed of keyword class
followed by the class name
• Class body: enclosed by a pair of curly braces.
General form of class declaration
• Class className
• {
• Access_specifier_1:
• Members;
• Access_specifier_2:
• Members;
• ……
• }
• objectName;
Cont…………
• The objectName is optional.
• If present, it declares objects of the class.
Example of student class
• Class student
• {
• Int rollNo; (data members)
• Char name [25];
• Char addr [35];
• Public:
• Void getdata(); (member functions)
• Void display();
• }
• ;
Cont………
• The class data members and member
functions are declared within the body of the
class along with their access levels.
• The class body defined the class member list
and their scope.
• If two classes have members with the same
name, the program will not show any error
since the members refer to different objects.
Access specifier
• Public
• Private
• Protected
Public
• Public members may be accessed by member
functions of same class and functions outside
the scope of the class (anywhere inside the
program)
Private
• Private members may only be accessed by
member functions and friend function of the
class.
• A class that enforces information hiding
declares its data members as private.
Protected
• The protected members may be accessed only
by the member functions of its class or by
member functions of its derived class.
Cont………..
• The data hiding is achieved by declaring data
members in the private section of the class.
• Since the private members are not accessible
from outside the class, they will be accessed
by the publicly declared member functions of
the same class.
Defining a class
class box
{
Public:
Int a,b,c; Data Members
void get()
{
cin>>a>>b>>c;
}
void put() Member Functions
{
cout<<a<<b<<c;
}
};
C++
IT 3rd Sem
Object-Oriented Programming:
Class:
class <class-name>
{
access-specifier:
variable declarations
function declarations
access-specifier:
variable declarations
function declarations
access-specifier:
variable declarations
function declarations
};
Example
• Class rectangle
• {
• Int length;
• Int width;
• Public:
• Void setvalues (int, int);
• Int area();
• };
Cont………..
• It declares a class rectangle. The class contains
four members:
• Two data members length and width of type
integer in the private section (because private
section is default permission)
• Two member functions:
• Setvalues (int, int) and area() in the public
section.
C++
IT 3rd Sem
Object-Oriented Programming:
Object:
class <class-name>
{
access-specifier:
variable declarations
function declarations
access-specifier:
variable declarations
function declarations
} ob1; //object creation
void main()
{
<class-name> ob2; //object
creation
Objects
A class provides the blueprints for objects, so basically an
object is created from a class. Object can be called as an
instance of a class. We declare objects of a class with exactly
the same sort of declaration that we declare variables of basic
types.
Following statements declare two objects of class Box:
Box Box1;
Box Box2;
Cont………..
• The definition of class does not cause any
storage to be allocated. Storage is only
allocated when object of class type is defined.
• The process of creating objects of the class is
called class instantiation.
Syntax of defining objects of a class
• Class className objectName
• Class : keyword
• ClassName : user defined class name
• User defined object name
Object Operations
All operations that can be applied to basic data types can be
applied to the objects.
E.g.:
• Arithematic
• Relational
• Logical
Example
• Class rectangle
• {
• Int length;
• Int width;
• Public:
• // member functions
• };
• The definition
• Rectangle rect;
Cont………..
Will allocate the memory space sufficient to
contain the two data members of the rectangle
class.
The name rect refers to that memory location.
Each class object has its own copy of the class
data members.
• An object of a class type also has a lifetime.
Data members
• Declared in the same way as the variables are
declared.
• Generally, all data members of a class are
made private to that class.
• If data members are declared in the public
section of the class, they will be accessed
using member access operator, dot (.).
Syntax of accessing data members of
class
• objectName . Data member
• ObjectName : user defined object name
• . : member access operator
• Data member: data member of a class.
Member functions
• Functions that are declared within a class are
called member functions.
• Works on data members of class.
• Member functions can access any element of
the class of which they belong to.
• The member functions of a class are declared
inside the class body.
Syntax of accessing member functions
of a class
• objectName . functionName (Actual
Arguments)
• objectName: user defined object name
• . : member access operator
• functionName: name of the member function
• Actual arguments: arguments list to the
function
Cont…………
• Member functions are declared within the
scope of their class. It means member
function is not visible outside the scope of its
class.
Member function can be defined in
two ways
• Inside the class
• Outside the class
Functions defined inside the class
• Member function of a class can be defined
inside the class declaration.
• Its syntax is similar to a normal function
definition except that it is enclosed within the
body of a class.
Example
• The member function in the rectangle class can be defined as follows:
• Class rectangle
• {
• Int length;
• Int width;
• Public:
• Void setvalues(int x, int y)
• {
• Length = x;
• Width = y;
• }
• Int area ()
• {
• return(length * width);
• }
• } ;
Cont…….
• In this the class contains two member
functions in the public section:
Setvalues(int, int)
Area()
• They are defined inside the class itself.
Functions defined outside the class
• In this method, the prototype of the member
function is declared within the body of the
class and then defined outside the body of the
class.
• Functions defined outside the class have the
same syntax as the normal function, there
should be a mechanism of binding to the class
to which they belong.
Cont……………
• This requires a special declaration.
• The name of the member function must be
qualified by the name of its class by using the
scope resolution operator. ( : : )
General format of member function
definition
• Class className
• {
• ……
• returnType memberFunction (arguments);
• User defined class name
• }
• ;
Member function definition outside
the class
• returnType className :: memberFunction
(arguments)
• {
• // body of the function
• }
Object-Oriented Programming
Using C++, Third Edition
42
Implementing Class Functions
• When you construct a class, you create two parts:
– Declaration section: contains the class name,
variables (attributes), and function prototypes
– Implementation section: contains the functions
• Use both the class name and the scope resolution
operator (::) when you implement a class function
Object-Oriented Programming
Using C++, Third Edition
43
Implementing Class Functions
(continued)
Object-Oriented Programming
Using C++, Third Edition
44
Using Static Class Members
• When a class field is static, only one memory
location is allocated
– All members of the class share a single storage
location for a static data member of that same class
• When you create a non-static variable within a
function, a new variable is created every time you
call that function
• When you create a static variable, the variable
maintains its memory address and previous value
for the life of the program
Static variable
• Static variables are sometimes called class
variables, class fields, or class-wide fields
because they don’t belong to a specific object;
they belong to the class.
• Initialized and allocated storage only once at
the beginning of the program execution.
• No matters how many times they are called
and used in the program.
• Retains its value until the end of the program.
Chapter 11 Starting Out with
C++: Early Objects 5/e
slide 46
© 2006 Pearson Education.
All Rights Reserved
Static Members
• Static variables:
- Shared by all objects of the class
- Like a “global variable” among objects of the
class
• Static member functions:
- Can be used to access static member
variables
- Can be called before any objects are
created
Chapter 11 Starting Out with
C++: Early Objects 5/e
slide 47
© 2006 Pearson Education.
All Rights Reserved
Constant Member Functions
• Declared with keyword const
• When const follows the parameter list,
int getX() const; (in the class definition)
int X::getX() const (defined outside the class)
the function is prevented from modifying the object
– can’t change the object attributes
• When const appears in the parameter list,
int setNum (const int num)
the function is prevented from modifying the
parameter. The parameter is read-only.
Chapter 11 Starting Out with
C++: Early Objects 5/e
slide 48
© 2006 Pearson Education.
All Rights Reserved
The this Pointer and Constant
Member Functions
• this pointer:
- Implicit parameter passed to a member
function (by the compiler)
- points to the object calling the function
• const member function:
- does not modify its calling object
This pointer
• When a member function is called, an implicit
argument is automatically passed that is a
pointer to the invoking object (that is, object
on which the function is called). This type of
pointer is called this.
Chapter 11 Starting Out with
C++: Early Objects 5/e
slide 50
© 2006 Pearson Education.
All Rights Reserved
Using the this Pointer
• Can be used to access members that may
be hidden by parameters with same name:
class SomeClass
{
private:
int num;
public:
void setNum(int num)
{ this->num = num; }
};
Constant keyword
• In c++, constant keyword is used to make
program elements constant. Constant keyword
can be used with:
• Variable
• Pointer
• Function arguments and return types
• Class data member
• Class member function
• objects
C++
IT 3rd Sem
Object-Oriented Programming:
Types of Member Functions:
 Inline Functions
 Nested Functions
 Friend Functions
 Static Functions
 Virtual Functions
FRIEND FUNCTION
• The protected and private members cannot be
accessed from outside the same class at which
they are declared.
• Its possible to grant a non member function
access to the private members of a class, by
using a keyword friend.
Cont………
• A friend function has access to all private and
protected members of the class for which it is
friend.
Syntax of friend function
• Class rectangle
• {
• ……
• …..
• Public:
• ……
• ……
• Friend rectangle duplicate (rectangle)
• }
Characteristics of friend function
1. It is not member function of the class.
2. It is like normal external functions.
3. It is not in the scope of the class to which it
has been declared.
4. It can be declared either public or private
section of the class.
5. Usually it passes objects as arguments.
Chapter 11 Starting Out with
C++: Early Objects 5/e
slide 57
© 2006 Pearson Education.
All Rights Reserved
Friends of Classes
• Friend function: a function that is not a
member of a class, but has access to private
members of the class
• A friend function can be 1) a stand-alone
function or 2) a member function of another
class
• It is declared a friend of a class with the
friend keyword in the function prototype
Chapter 11 Starting Out with
C++: Early Objects 5/e
slide 58
© 2006 Pearson Education.
All Rights Reserved
Friend Class Declaration
3) An entire class can be declared a friend of a
class:
class aClass
{private:
int x;
friend class frClass;
};
class frClass
{public:
void fSet(aClass &c,int a){c.x = a;}
int fGet(aClass c){return c.x;}
};
Chapter 11 Starting Out with
C++: Early Objects 5/e
slide 59
© 2006 Pearson Education.
All Rights Reserved
Friend Class Declaration
• If frClass is a friend of aClass, then all
member functions of frClass have
unrestricted access to all members of
aClass, including the private members.
• In general, restrict the property of Friendship
to only those functions that must have access
to the private members of a class.
Nested class
• A class can be defined within other class, such
a class is called nested class.
• A member of its enclosing class.
• Its definition can occur within a public,
protected or private section of its enclosing
class.
Local classes
• A class that can be defined inside a function
body. Such a class is called local class.
• It is only visible in the local scope in which it is
defined.
Abstract class
• An abstract class is a class that is designed to
be specifically used as a base class.
• An abstract class contains at least one pure
virtual function.
• You declare a pure virtual function by using
a pure specifier (= 0) in the declaration of a
virtual member function in the class
declaration.
Cont………..
• You cannot create an object of an abstract
class type; however, you can use pointers and
references to abstract class types.
• A class that contains at least one pure virtual
function is considered an abstract class.
• Used to provide an interface to its sub classes.
Pure virtual functions
• Functions with no definition.
• They start with keyword virtual and ends with
= 0
• Syntax is:
• Virtual void f() = 0;
container classes
• A Container class is defined as a class that
gives you the power to store any type of data.
• There are two type of container classes in C++,
namely
• “Simple Container Classes” and
• “Associative Container Classes”.
• An Associative Container class associates a key
to each object to minimize the average access
time.
Cont………
• Simple Container Classes
* vector<>
* lists<>
* stack<>
* queue<>
* deque<>
Cont…………
• Associative Container Classes
* map<>
* set<>
* multimap<>
* multiset<>
Storage classes
• Used to specify the lifetime and scope of the
variables.
• How storage is allocated for variables and how
variable is treated by complier depending
upon these storage classes.
5 types
1. Local variable
2. Global variable
3. Register variable
4. Extern variable
5. Static variable
Namespace
• Container for identifiers.
• Puts the names of its member in a distinct
space so that they don’t conflict with the
names in other namespaces.
Syntax
• Its creation is similar to class creation
• Namespace Myspace
• {
• Declarations
• }
• Int main() {}
Will create namespace named Myspace, in
which we put member declarations.
Class member function
• A member function of a class is a function that
has its definition or its prototype within the
class definition like any other variable.
Class access modifier
• A class member can be defined as public,
private or protected. By default members
would be assumed as private.

More Related Content

What's hot

OOP Introduction with java programming language
OOP Introduction with java programming languageOOP Introduction with java programming language
OOP Introduction with java programming languageMd.Al-imran Roton
 
Friend function & friend class
Friend function & friend classFriend function & friend class
Friend function & friend classAbhishek Wadhwa
 
Basic concepts of object oriented programming
Basic concepts of object oriented programmingBasic concepts of object oriented programming
Basic concepts of object oriented programmingSachin Sharma
 
Introduction to method overloading &amp; method overriding in java hdm
Introduction to method overloading &amp; method overriding  in java  hdmIntroduction to method overloading &amp; method overriding  in java  hdm
Introduction to method overloading &amp; method overriding in java hdmHarshal Misalkar
 
Object oriented programming concepts
Object oriented programming conceptsObject oriented programming concepts
Object oriented programming conceptsrahuld115
 
Pointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cppPointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cpprajshreemuthiah
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++Vishal Patil
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++Muhammad Waqas
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Michelle Anne Meralpis
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++HalaiHansaika
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methodsShubham Dwivedi
 
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 Kamlesh Makvana
 

What's hot (20)

OOP Introduction with java programming language
OOP Introduction with java programming languageOOP Introduction with java programming language
OOP Introduction with java programming language
 
Basic concept of OOP's
Basic concept of OOP'sBasic concept of OOP's
Basic concept of OOP's
 
Friend function & friend class
Friend function & friend classFriend function & friend class
Friend function & friend class
 
Basic concepts of object oriented programming
Basic concepts of object oriented programmingBasic concepts of object oriented programming
Basic concepts of object oriented programming
 
Python programming : Classes objects
Python programming : Classes objectsPython programming : Classes objects
Python programming : Classes objects
 
Vectors in Java
Vectors in JavaVectors in Java
Vectors in Java
 
OOPS Basics With Example
OOPS Basics With ExampleOOPS Basics With Example
OOPS Basics With Example
 
Introduction to method overloading &amp; method overriding in java hdm
Introduction to method overloading &amp; method overriding  in java  hdmIntroduction to method overloading &amp; method overriding  in java  hdm
Introduction to method overloading &amp; method overriding in java hdm
 
Object oriented programming concepts
Object oriented programming conceptsObject oriented programming concepts
Object oriented programming concepts
 
Pointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cppPointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cpp
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Python Functions
Python   FunctionsPython   Functions
Python Functions
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)
 
OOP java
OOP javaOOP java
OOP java
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
 
Pointer in C++
Pointer in C++Pointer in C++
Pointer in C++
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methods
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
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
 

Viewers also liked (20)

Object as function argument , friend and static function by shahzad younas
Object as function argument , friend and static function by shahzad younasObject as function argument , friend and static function by shahzad younas
Object as function argument , friend and static function by shahzad younas
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).ppt
 
Bca 2nd sem u-1 iintroduction
Bca 2nd sem u-1 iintroductionBca 2nd sem u-1 iintroduction
Bca 2nd sem u-1 iintroduction
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Algorithms & flowcharts
Algorithms & flowchartsAlgorithms & flowcharts
Algorithms & flowcharts
 
User Defined Functions
User Defined FunctionsUser Defined Functions
User Defined Functions
 
System's Specification
System's SpecificationSystem's Specification
System's Specification
 
Pointers
PointersPointers
Pointers
 
How to choose best containers in STL (C++)
How to choose best containers in STL (C++)How to choose best containers in STL (C++)
How to choose best containers in STL (C++)
 
Programming In C++
Programming In C++ Programming In C++
Programming In C++
 
Classes And Objects
Classes And ObjectsClasses And Objects
Classes And Objects
 
1. introduction to semantics
1. introduction to semantics1. introduction to semantics
1. introduction to semantics
 
Flowchart
FlowchartFlowchart
Flowchart
 
C++ Pointers
C++ PointersC++ Pointers
C++ Pointers
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)
 
Unit 6 pointers
Unit 6   pointersUnit 6   pointers
Unit 6 pointers
 
functions of C++
functions of C++functions of C++
functions of C++
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
 
Data Structures and Algorithms
Data Structures and AlgorithmsData Structures and Algorithms
Data Structures and Algorithms
 
SEMANTICS
SEMANTICS SEMANTICS
SEMANTICS
 

Similar to Classes and objects

c++ lecture 1
c++ lecture 1c++ lecture 1
c++ lecture 1sai kumar
 
c++ lecture 1
c++ lecture 1c++ lecture 1
c++ lecture 1sai kumar
 
c++ introduction
c++ introductionc++ introduction
c++ introductionsai kumar
 
Concept of Object-Oriented in C++
Concept of Object-Oriented in C++Concept of Object-Oriented in C++
Concept of Object-Oriented in C++Abdullah Jan
 
classandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.pptclassandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.pptmanomkpsg
 
[OOP - Lec 09,10,11] Class Members & their Accessing
[OOP - Lec 09,10,11] Class Members & their Accessing[OOP - Lec 09,10,11] Class Members & their Accessing
[OOP - Lec 09,10,11] Class Members & their AccessingMuhammad Hammad Waseem
 
static members in object oriented program.pptx
static members in object oriented program.pptxstatic members in object oriented program.pptx
static members in object oriented program.pptxurvashipundir04
 
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
 
class and object C++ language chapter 2.pptx
class and object C++ language chapter 2.pptxclass and object C++ language chapter 2.pptx
class and object C++ language chapter 2.pptxAshrithaRokkam
 
L5 classes, objects, nested and inner class
L5 classes, objects, nested and inner classL5 classes, objects, nested and inner class
L5 classes, objects, nested and inner classteach4uin
 
Data members and member functions
Data members and member functionsData members and member functions
Data members and member functionsHarsh Patel
 
C++ training
C++ training C++ training
C++ training PL Sharma
 
oop lecture 3
oop lecture 3oop lecture 3
oop lecture 3Atif Khan
 
OOP Unit 2 - Classes and Object
OOP Unit 2 - Classes and ObjectOOP Unit 2 - Classes and Object
OOP Unit 2 - Classes and Objectdkpawar
 

Similar to Classes and objects (20)

Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
c++ lecture 1
c++ lecture 1c++ lecture 1
c++ lecture 1
 
c++ lecture 1
c++ lecture 1c++ lecture 1
c++ lecture 1
 
c++ introduction
c++ introductionc++ introduction
c++ introduction
 
Class and objects
Class and objectsClass and objects
Class and objects
 
Classes
ClassesClasses
Classes
 
Concept of Object-Oriented in C++
Concept of Object-Oriented in C++Concept of Object-Oriented in C++
Concept of Object-Oriented in C++
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
classandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.pptclassandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.ppt
 
[OOP - Lec 09,10,11] Class Members & their Accessing
[OOP - Lec 09,10,11] Class Members & their Accessing[OOP - Lec 09,10,11] Class Members & their Accessing
[OOP - Lec 09,10,11] Class Members & their Accessing
 
static members in object oriented program.pptx
static members in object oriented program.pptxstatic members in object oriented program.pptx
static members in object oriented program.pptx
 
Class and object
Class and objectClass and object
Class and object
 
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 and object C++ language chapter 2.pptx
class and object C++ language chapter 2.pptxclass and object C++ language chapter 2.pptx
class and object C++ language chapter 2.pptx
 
L5 classes, objects, nested and inner class
L5 classes, objects, nested and inner classL5 classes, objects, nested and inner class
L5 classes, objects, nested and inner class
 
Data members and member functions
Data members and member functionsData members and member functions
Data members and member functions
 
C++ training
C++ training C++ training
C++ training
 
oop lecture 3
oop lecture 3oop lecture 3
oop lecture 3
 
OOP Unit 2 - Classes and Object
OOP Unit 2 - Classes and ObjectOOP Unit 2 - Classes and Object
OOP Unit 2 - Classes and Object
 
VB.net&OOP.pptx
VB.net&OOP.pptxVB.net&OOP.pptx
VB.net&OOP.pptx
 

Recently uploaded

AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Q4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxQ4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxnelietumpap1
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxMaryGraceBautista27
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 

Recently uploaded (20)

AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
Q4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxQ4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptx
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptx
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 

Classes and objects

  • 1. CLASSES AND OBJECTS ANIL KUMAR https://www.facebook.com/AniLK0221
  • 2. INTRODUCTION • C++ class mechanism allows users to define their own data types that can be used as conveniently as build in types. • Classes are often called user defined types. • Includes defining of class types and creation of objects of classes.
  • 3. Cont………… • The class definition introduces both : • the class data members that define the internal representation of class • The class member functions that define the set of operations that may be applied to objects of class type.
  • 4. Cont…………. • Information hiding is achieved by declaring the data members as private whereas the operations to be performed on class objects by the program are public.
  • 5. What is class? • Created using keyword class. • A class declaration defines a new user defined data type that links code and data. • The new data type is then used to declare objects of that class. • Class is a logical abstraction but an object has physical existence. • Objects is an instance of class.
  • 6. Cont………. • The class is the cornerstone of C++ – It makes possible encapsulation, data hiding and inheritance – A user defined type – Consists of both data and methods – Defines properties and behavior of that type
  • 7. Syntax of Class declaration • Class className • { • // body of a class • }
  • 8. Cont……………. • The class definition has two parts: • Class head : composed of keyword class followed by the class name • Class body: enclosed by a pair of curly braces.
  • 9. General form of class declaration • Class className • { • Access_specifier_1: • Members; • Access_specifier_2: • Members; • …… • } • objectName;
  • 10. Cont………… • The objectName is optional. • If present, it declares objects of the class.
  • 11. Example of student class • Class student • { • Int rollNo; (data members) • Char name [25]; • Char addr [35]; • Public: • Void getdata(); (member functions) • Void display(); • } • ;
  • 12. Cont……… • The class data members and member functions are declared within the body of the class along with their access levels. • The class body defined the class member list and their scope. • If two classes have members with the same name, the program will not show any error since the members refer to different objects.
  • 13. Access specifier • Public • Private • Protected
  • 14. Public • Public members may be accessed by member functions of same class and functions outside the scope of the class (anywhere inside the program)
  • 15. Private • Private members may only be accessed by member functions and friend function of the class. • A class that enforces information hiding declares its data members as private.
  • 16. Protected • The protected members may be accessed only by the member functions of its class or by member functions of its derived class.
  • 17. Cont……….. • The data hiding is achieved by declaring data members in the private section of the class. • Since the private members are not accessible from outside the class, they will be accessed by the publicly declared member functions of the same class.
  • 18. Defining a class class box { Public: Int a,b,c; Data Members void get() { cin>>a>>b>>c; } void put() Member Functions { cout<<a<<b<<c; } };
  • 19. C++ IT 3rd Sem Object-Oriented Programming: Class: class <class-name> { access-specifier: variable declarations function declarations access-specifier: variable declarations function declarations access-specifier: variable declarations function declarations };
  • 20. Example • Class rectangle • { • Int length; • Int width; • Public: • Void setvalues (int, int); • Int area(); • };
  • 21. Cont……….. • It declares a class rectangle. The class contains four members: • Two data members length and width of type integer in the private section (because private section is default permission) • Two member functions: • Setvalues (int, int) and area() in the public section.
  • 22. C++ IT 3rd Sem Object-Oriented Programming: Object: class <class-name> { access-specifier: variable declarations function declarations access-specifier: variable declarations function declarations } ob1; //object creation void main() { <class-name> ob2; //object creation
  • 23. Objects A class provides the blueprints for objects, so basically an object is created from a class. Object can be called as an instance of a class. We declare objects of a class with exactly the same sort of declaration that we declare variables of basic types. Following statements declare two objects of class Box: Box Box1; Box Box2;
  • 24. Cont……….. • The definition of class does not cause any storage to be allocated. Storage is only allocated when object of class type is defined. • The process of creating objects of the class is called class instantiation.
  • 25. Syntax of defining objects of a class • Class className objectName • Class : keyword • ClassName : user defined class name • User defined object name
  • 26. Object Operations All operations that can be applied to basic data types can be applied to the objects. E.g.: • Arithematic • Relational • Logical
  • 27. Example • Class rectangle • { • Int length; • Int width; • Public: • // member functions • }; • The definition • Rectangle rect;
  • 28. Cont……….. Will allocate the memory space sufficient to contain the two data members of the rectangle class. The name rect refers to that memory location. Each class object has its own copy of the class data members. • An object of a class type also has a lifetime.
  • 29. Data members • Declared in the same way as the variables are declared. • Generally, all data members of a class are made private to that class. • If data members are declared in the public section of the class, they will be accessed using member access operator, dot (.).
  • 30. Syntax of accessing data members of class • objectName . Data member • ObjectName : user defined object name • . : member access operator • Data member: data member of a class.
  • 31. Member functions • Functions that are declared within a class are called member functions. • Works on data members of class. • Member functions can access any element of the class of which they belong to. • The member functions of a class are declared inside the class body.
  • 32. Syntax of accessing member functions of a class • objectName . functionName (Actual Arguments) • objectName: user defined object name • . : member access operator • functionName: name of the member function • Actual arguments: arguments list to the function
  • 33. Cont………… • Member functions are declared within the scope of their class. It means member function is not visible outside the scope of its class.
  • 34. Member function can be defined in two ways • Inside the class • Outside the class
  • 35. Functions defined inside the class • Member function of a class can be defined inside the class declaration. • Its syntax is similar to a normal function definition except that it is enclosed within the body of a class.
  • 36. Example • The member function in the rectangle class can be defined as follows: • Class rectangle • { • Int length; • Int width; • Public: • Void setvalues(int x, int y) • { • Length = x; • Width = y; • } • Int area () • { • return(length * width); • } • } ;
  • 37. Cont……. • In this the class contains two member functions in the public section: Setvalues(int, int) Area() • They are defined inside the class itself.
  • 38. Functions defined outside the class • In this method, the prototype of the member function is declared within the body of the class and then defined outside the body of the class. • Functions defined outside the class have the same syntax as the normal function, there should be a mechanism of binding to the class to which they belong.
  • 39. Cont…………… • This requires a special declaration. • The name of the member function must be qualified by the name of its class by using the scope resolution operator. ( : : )
  • 40. General format of member function definition • Class className • { • …… • returnType memberFunction (arguments); • User defined class name • } • ;
  • 41. Member function definition outside the class • returnType className :: memberFunction (arguments) • { • // body of the function • }
  • 42. Object-Oriented Programming Using C++, Third Edition 42 Implementing Class Functions • When you construct a class, you create two parts: – Declaration section: contains the class name, variables (attributes), and function prototypes – Implementation section: contains the functions • Use both the class name and the scope resolution operator (::) when you implement a class function
  • 43. Object-Oriented Programming Using C++, Third Edition 43 Implementing Class Functions (continued)
  • 44. Object-Oriented Programming Using C++, Third Edition 44 Using Static Class Members • When a class field is static, only one memory location is allocated – All members of the class share a single storage location for a static data member of that same class • When you create a non-static variable within a function, a new variable is created every time you call that function • When you create a static variable, the variable maintains its memory address and previous value for the life of the program
  • 45. Static variable • Static variables are sometimes called class variables, class fields, or class-wide fields because they don’t belong to a specific object; they belong to the class. • Initialized and allocated storage only once at the beginning of the program execution. • No matters how many times they are called and used in the program. • Retains its value until the end of the program.
  • 46. Chapter 11 Starting Out with C++: Early Objects 5/e slide 46 © 2006 Pearson Education. All Rights Reserved Static Members • Static variables: - Shared by all objects of the class - Like a “global variable” among objects of the class • Static member functions: - Can be used to access static member variables - Can be called before any objects are created
  • 47. Chapter 11 Starting Out with C++: Early Objects 5/e slide 47 © 2006 Pearson Education. All Rights Reserved Constant Member Functions • Declared with keyword const • When const follows the parameter list, int getX() const; (in the class definition) int X::getX() const (defined outside the class) the function is prevented from modifying the object – can’t change the object attributes • When const appears in the parameter list, int setNum (const int num) the function is prevented from modifying the parameter. The parameter is read-only.
  • 48. Chapter 11 Starting Out with C++: Early Objects 5/e slide 48 © 2006 Pearson Education. All Rights Reserved The this Pointer and Constant Member Functions • this pointer: - Implicit parameter passed to a member function (by the compiler) - points to the object calling the function • const member function: - does not modify its calling object
  • 49. This pointer • When a member function is called, an implicit argument is automatically passed that is a pointer to the invoking object (that is, object on which the function is called). This type of pointer is called this.
  • 50. Chapter 11 Starting Out with C++: Early Objects 5/e slide 50 © 2006 Pearson Education. All Rights Reserved Using the this Pointer • Can be used to access members that may be hidden by parameters with same name: class SomeClass { private: int num; public: void setNum(int num) { this->num = num; } };
  • 51. Constant keyword • In c++, constant keyword is used to make program elements constant. Constant keyword can be used with: • Variable • Pointer • Function arguments and return types • Class data member • Class member function • objects
  • 52. C++ IT 3rd Sem Object-Oriented Programming: Types of Member Functions:  Inline Functions  Nested Functions  Friend Functions  Static Functions  Virtual Functions
  • 53. FRIEND FUNCTION • The protected and private members cannot be accessed from outside the same class at which they are declared. • Its possible to grant a non member function access to the private members of a class, by using a keyword friend.
  • 54. Cont……… • A friend function has access to all private and protected members of the class for which it is friend.
  • 55. Syntax of friend function • Class rectangle • { • …… • ….. • Public: • …… • …… • Friend rectangle duplicate (rectangle) • }
  • 56. Characteristics of friend function 1. It is not member function of the class. 2. It is like normal external functions. 3. It is not in the scope of the class to which it has been declared. 4. It can be declared either public or private section of the class. 5. Usually it passes objects as arguments.
  • 57. Chapter 11 Starting Out with C++: Early Objects 5/e slide 57 © 2006 Pearson Education. All Rights Reserved Friends of Classes • Friend function: a function that is not a member of a class, but has access to private members of the class • A friend function can be 1) a stand-alone function or 2) a member function of another class • It is declared a friend of a class with the friend keyword in the function prototype
  • 58. Chapter 11 Starting Out with C++: Early Objects 5/e slide 58 © 2006 Pearson Education. All Rights Reserved Friend Class Declaration 3) An entire class can be declared a friend of a class: class aClass {private: int x; friend class frClass; }; class frClass {public: void fSet(aClass &c,int a){c.x = a;} int fGet(aClass c){return c.x;} };
  • 59. Chapter 11 Starting Out with C++: Early Objects 5/e slide 59 © 2006 Pearson Education. All Rights Reserved Friend Class Declaration • If frClass is a friend of aClass, then all member functions of frClass have unrestricted access to all members of aClass, including the private members. • In general, restrict the property of Friendship to only those functions that must have access to the private members of a class.
  • 60. Nested class • A class can be defined within other class, such a class is called nested class. • A member of its enclosing class. • Its definition can occur within a public, protected or private section of its enclosing class.
  • 61. Local classes • A class that can be defined inside a function body. Such a class is called local class. • It is only visible in the local scope in which it is defined.
  • 62. Abstract class • An abstract class is a class that is designed to be specifically used as a base class. • An abstract class contains at least one pure virtual function. • You declare a pure virtual function by using a pure specifier (= 0) in the declaration of a virtual member function in the class declaration.
  • 63. Cont……….. • You cannot create an object of an abstract class type; however, you can use pointers and references to abstract class types. • A class that contains at least one pure virtual function is considered an abstract class. • Used to provide an interface to its sub classes.
  • 64. Pure virtual functions • Functions with no definition. • They start with keyword virtual and ends with = 0 • Syntax is: • Virtual void f() = 0;
  • 65. container classes • A Container class is defined as a class that gives you the power to store any type of data. • There are two type of container classes in C++, namely • “Simple Container Classes” and • “Associative Container Classes”. • An Associative Container class associates a key to each object to minimize the average access time.
  • 66. Cont……… • Simple Container Classes * vector<> * lists<> * stack<> * queue<> * deque<>
  • 67. Cont………… • Associative Container Classes * map<> * set<> * multimap<> * multiset<>
  • 68. Storage classes • Used to specify the lifetime and scope of the variables. • How storage is allocated for variables and how variable is treated by complier depending upon these storage classes.
  • 69. 5 types 1. Local variable 2. Global variable 3. Register variable 4. Extern variable 5. Static variable
  • 70. Namespace • Container for identifiers. • Puts the names of its member in a distinct space so that they don’t conflict with the names in other namespaces.
  • 71. Syntax • Its creation is similar to class creation • Namespace Myspace • { • Declarations • } • Int main() {} Will create namespace named Myspace, in which we put member declarations.
  • 72. Class member function • A member function of a class is a function that has its definition or its prototype within the class definition like any other variable.
  • 73. Class access modifier • A class member can be defined as public, private or protected. By default members would be assumed as private.