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

Classes and Objects

The document provides an overview of classes and objects in C++. It discusses key concepts like class declaration, creating objects, accessing members, defining member functions, nesting member functions, and access specifiers like public and private. Some key points: - Classes allow defining data (members) and associated functions together, with class declaration describing types/scopes and function definitions implementing behaviors. - Objects are created from classes and allocate separate memory for member variables but not functions. - Member functions can be defined inside or outside the class. Those defined inside don't need a prototype. - Access specifiers like public allow external access to members while private hides them within the class. - Nested classes declare

Uploaded by

Lokendra Kapoor
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

Classes and Objects

The document provides an overview of classes and objects in C++. It discusses key concepts like class declaration, creating objects, accessing members, defining member functions, nesting member functions, and access specifiers like public and private. Some key points: - Classes allow defining data (members) and associated functions together, with class declaration describing types/scopes and function definitions implementing behaviors. - Objects are created from classes and allocate separate memory for member variables but not functions. - Member functions can be defined inside or outside the class. Those defined inside don't need a prototype. - Access specifiers like public allow external access to members while private hides them within the class. - Nested classes declare

Uploaded by

Lokendra Kapoor
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Lecture 4-7

Classes and Objects


Contents

• Introduction to Classes and Objects


• Class declaration
• Creating Objects
• Accessing Object Members
• Defining Member Functions
• Nesting of Member Function
• Access Specifiers: Public , Private
• Nested Classes
Classes

• Different from structures


– Adds member functions
– Not just member data

• Integral to object-oriented programming


– Focus on objects
• Object: Contains data and operations
• In C++, variables of class type are objects
Specifying a Class

• A class is a way to bind the data and its associated


functions together.
• A class specification has 2 parts:
– Class declaration
– Class function definitions

• Class declaration: describes type and scope of its members.


• Class function definitions: describes how the class functions
are implemented.
General form of Class declaration
Data Hiding
Difference between Structure and Class

• By default, members of class are private, while, by


default, members of structure are public.

• Encapsulation

• The keywords public and private are known as


visibility labels.
A Simple Class Example
class item
{
int number;
float cost;
public :
void getdata(int a, float b);
void putdata (void);
};
Class Representation

Creating Objects:
item x;

Accessing Class Members:


Object_name.function-name (actual-arguments);
x.getdata(100, 75.5); x.putdata( );
Creating Objects

• Objects can also be created as follows:


Memory Allocation for Objects

• Memory space for objects is allocated when they are


declared and not when the class is specified :
partially true.
• Since all the objects belonging to that class use the
same member functions, no separate space is
allocated for member functions when the objects are
created.
• Only space for member variables is allocated
separately for each object. Because, member
variables will hold different data values for different
objects.
Example
Defining Member Functions
• Outside the class definition • Inside the class definition
When function is defined outside class, it When function is defined inside class, it
requires a prototype declaration in the class. does not require a prototype declaration in
the class.
Inline Functions
• We can define a member function outside the class definition
and still make it inline by using the qualifier inline in the
header line of the function definition.
Nesting of Member Functions

• An object of a class using dot operator generally calls a


member function of the class.

• However, a member function may also be called by using its


name inside another member function of the same class.

• This is known as “Nesting of Member Functions”


Nesting of Member Functions
Data Hiding Revisited
Access Specifier: Public

• Public members are accessible outside the class.

• Public members are accessible to both member Functions and


non-member Functions.

• Objects can access the members directly using dot operator.


E.g.
– object name.public data member
– object name.public member function
Access Specifier: Public
Access Specifier: Private

• Private Members can only be accessed by the member


functions of that class.

• They cannot be accessed by any non member functions (data is


hidden from outside world).

• Object of a class cannot access private members using dot


operators.

• All data at the beginning of a class is by default private, until


any specifier is mentioned.
Access Specifier: Private
Private Member Functions
Private Member Functions
Nested Classes
• A class is declared within another class.

• The outer class is called enclosing class and inner


class is called the nested class.

• A nested class is a member and as such has the


same access rights as any other member.

• The members of an enclosing class have no


special access to members of a nested class; the
usual access rules shall be obeyed.
Nested Classes Example
#include<iostream>
using namespace std;
class Enclosing /* start of Enclosing class declaration */
{
int x;
class Nested
{ /* start of Nested class declaration */
int y;
void NestedFun(Enclosing e)
{
cout<<e.x; // works fine: nested class can access
// private members of Enclosing class
}
}; // declaration Nested class ends here
}; // declaration Enclosing class ends here
int main(){}
Nested Classes Example
#include<iostream>
using namespace std;
class Enclosing /* start of Enclosing class declaration */
{
int x;
class Nested
{ /* start of Nested class declaration */
int y;
}; // declaration Nested class ends here
void EnclosingFun(Nested n)
{
cout<<n.y; // Compiler Error: y is private in Nested
}
}; // declaration Enclosing class ends here
int main(){}
Thank You

You might also like