0% found this document useful (0 votes)
223 views

Classes in C++ Full

1. A class defines a user-defined data type by grouping together data members (variables) and member functions (methods) that act on those data members. 2. Member functions access class data members using the dot operator (e.g. object.function()) and can be declared as public, private, or protected to control access. 3. Special member functions called constructors are automatically called when an object is instantiated and are used to initialize the object. Destructors are called when an object is destroyed.

Uploaded by

Rohan Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
223 views

Classes in C++ Full

1. A class defines a user-defined data type by grouping together data members (variables) and member functions (methods) that act on those data members. 2. Member functions access class data members using the dot operator (e.g. object.function()) and can be declared as public, private, or protected to control access. 3. Special member functions called constructors are automatically called when an object is instantiated and are used to initialize the object. Destructors are called when an object is destroyed.

Uploaded by

Rohan Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 33

CLASSES

Introduction

• A Class is a user defined data-type which have data members


and member functions.
• Data members are the data variables and member functions
are the functions used to manipulate these variables and
together these data members and member functions defines
the properties and behavior of the objects in a Class.
Syntax
Class access specifier
Access specifier

Private :- Accessed only by the functions within the class not outside
the class
• Private functions – they are called by other member functions only
• Default :- private

Public
Data or functions can be directly referenced outside the class
Example
Declaration of fraction class
class Fraction
{
private:
int numerator;
int denominator;
public :
void store(int numer , int denom);
void print ();
};
Scope Resolution operator
• Syntax :-

Class_name ::member_name
• To refer to not in the current scope

Book img
i.e function definition is out of scope as it is defined
outside the class
Class Objects

• Also called Instantiation


(i.e creating an instance)

We declare objects of a class with exactly the same sort of declaration that we
declare variables of basic types.

• Example
Fraction fr;
Accessing member Object

• To refer to members within an object, we use the member


operator,which is simple a dot operator(.)
SYNTAX
objectID.memberID
Example
Fraction fr;
fr.print();
fr.store(20,50);
Class
Instantiation
Const function

• When a function is declared as const, it can be called on any


type of object, const object as well as non-const objects.
• Whenever an object is declared as const, it needs to be
initialized at the time of declaration. however, the object
initialization while declaring is possible only with the help of
constructors.
• A function becomes const when the const keyword is used in
the function’s declaration. The idea of const functions is not to
allow them to modify the object on which they are called. It
is recommended the practice to make as many functions const
as possible so that accidental changes to objects are avoided.
This pointer

• When we call a class member function it automatically


contains a hidden pointer known as this.
• This pointer contains the address of the invoking ()host)
object so that it can refer to data and other functions in the
class.
Characteristics:-
• It is a constant pointer , which means that we cannot change
its contents
• Most of the time it is used implicitly – that is hidden.
• We can use it explicitly when we need to access the current
host object
What is Constant pointer
MANAGER FUNCTIONS
MANAGER
FINCTIONS

COPY
CONSTRUCTOR
CONSTRUCTOR
DESTRUCTOR

The only functions in a class that are defined and declared without a
return value
CONSTRUCTOR

• They are special member functions that


are called when an instance of a class is
created. Whenever an object is
instantiated, a constructor is called.
•A class may have more than one
constructor
Uses of constructor:-
1) A constructor initializes class data members when required.
The data members of the class cannot be initialized in the
class declaration because each created object has its own
variables and must be initialized with values required by the
object being created.
2) Constructors can be used to validate data.
3) Constructor must be used to allocate the memory for an
object.
Format
Calling a constructor
• Fraction fr1;
• Fraction fr2(3);
• Fraction fr3(3,4);
• Definition of the object automatically calls the
appropriate overloaded constructor.
• Fraction fr1; // correct
• Fraction fr1(); // error
Note :- a class must
have at least one Note :- The default
constructor , either constructor is called
defined by the whenever an object is
program or by the created without
compiler passing any arguments.

Note :- If we define any


type of constructor , we
must also define a
default constructor.
COPY CONSTRUCTOR

• It is a function that is called whenever a copy of an existing


instance needs to be created
Bitwise copy(default) and Logical
copy
Note :- There is only
one copy constructor,
Copy constructor either defined by us
or by the system.

Fraction :: Fraction (const Fraction & copyfrom)


{
Numerator = copyfrom.numerator;
Denominator = copyfrom.denominator;
}
DESTRUCTOR

• They come into play when an object dies, either because it is no longer in scope
or because it has been deleted.
• Name of the destructor is the name of the class, preceded by (~)tilde.
• No return type.
• Destructor must have no parameters
• Cannot overload a destructor
• Class can have one ands only one destructor.
Eg :-
Fraction :: ~Fraction()
{ }
MUTATOR and ACCESSOR functions

Mutator function:-
A function that can change the state of the host object.
Fr1.store(4,9);
Accessor function:-
Cannot change the state of the invoking object.
An accessor is called either for its side effects such as to print or
for its return value.
Like mutators , accessor must always have a return type , even
if it is void.
Note

• To ensure that an accessor does not change the state of the


host object , we make the function itself constant by adding
the keyword const at the end of declaration and definition

void print () const;// declaration


Void Fraction :: print () const // definition
{…….}

You might also like