0% found this document useful (0 votes)
10 views39 pages

PPT3 Class Object

The document provides an overview of classes and objects in C++, detailing their definitions, member functions, access specifiers, and memory allocation. It explains the differences between classes and structures, the concept of encapsulation, and the use of inline functions. Additionally, it covers the scope resolution operator and nested classes, illustrating these concepts with examples.

Uploaded by

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

PPT3 Class Object

The document provides an overview of classes and objects in C++, detailing their definitions, member functions, access specifiers, and memory allocation. It explains the differences between classes and structures, the concept of encapsulation, and the use of inline functions. Additionally, it covers the scope resolution operator and nested classes, illustrating these concepts with examples.

Uploaded by

rajbirsinghshah6
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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
• Scope Resolution Operator
• Nested Classes
Objects and Classes
• An object is any thing. A class consists of a category of
things. An object is a specific item that belongs to a
class; it is called an instance of a class. A class defines
the characteristics of its objects and the methods that
can be applied to its objects.
• Each button on the toolbar of a word-processing
program is an instance of a Button class, and each
button shares some general characteristics that all
buttons possess. Even if you have never used a
particular piece of software before, if you are
presented with a screen containing a button, you know
how to use it.
Objects and Classes
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);
[Link](100, 75.5); [Link]( );
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.
return-type class-name::fun-name(args class item
list)
{
{
int number;
//Body of function
int cost;
}
public:
void item::setData(int a, int b) void setData(int a, int b)
{ {
number=a;
number=a;
cost=b;
cost=b; }
} }
Functions
• Benefits of Functions:
– Avoids rewriting of same code
– Easy to read
– Easy to modify
– Easy to debug
– Better memory utilization
Memory
• Functions in a program is to save memory
space which becomes appreciable when a
function is likely to be called many times.
Function is time consuming
• Every time a function is called it takes a lot of
extra time in executing a services of
instructions for task such as jumping to
functions, saving register, pushing arguments
to the stack and returning to the calling
function.
• So when a function is called it is worthless to
spend so much extra time in such tasks in cost
of saving comparatively small space.
Inline Function
• To eliminate the cost of calls to small
functions, C++ new feature called Inline
functions.
• A Inline function is a function that is expanded
in line when it is invoked.
• Compiler replaces the function call with the
corresponding function code.
Inline is a Request
• Inline is a request, not a command.
• The benefits of inline function reduces as the
function grows in size.
• Therefore, the compiler may ignore the
request in some cases. Few of the cases are:
– Function containing loops, switch, goto.
– Functions with recursion
– Containing static variables.
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.

class item{
private: int a
public:
void getData(int b);
};
inline void item::getResult(int b)
{
cost = a*b;
}
Nesting of Member Functions

• A member function calling another member functions


is known as nesting of member functions.
• A member function of a class can be called only by an
object of that class using a dot operator.
• However, there is an exception to this. A member
function can be called by using its name inside another
member function of the same class.
• Private member functions can also be accessed by
using nesting of member functions.
Nesting of Member Functions
class Employee{
string name;
public:
void display(void){
cout<<“name:”<<name;}
void getData(){ A member function of a
cin>>name; class can be called by
display(); another member
} function of the same
}; class. getData() is calling
int main(){ display().
Employee emp;
[Link]();
}
Access Specifiers in C++

3 types of access modifiers available in C++:

• Public

• Private (by default)

• Protected
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 [Link] data member
– object [Link] member function
Access Specifier: Public
class Employee{
private: int main(){
string name; Employee emp;
public:
string address; [Link] //ok
void setData(){
cin>>name;
cin>>address;} [Link]() //ok
void display(){
cout<<name;
cout<<address; [Link]() //ok
} }
};
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
class Employee{
private: int main(){
string name; Employee emp;
float salary;
float getSalary(){ cin>>[Link] //Not ok
return salary; }
public:
string address; [Link]() //ok
void setData(){
cin>>name;
cin>>address;} [Link]() //ok
void display(){ }
cout<<name;
cout<<address;}
};
Private Member Functions
class Employee{
private: int main(){
string name; Employee emp;
float salary;
float getSalary(){ cin>>[Link]; //NOT OK
return salary; } [Link](); // NOT OK
public: [Link]() //OK
string address;
void setData(){ [Link]() //OK
cin>>name; }
cin>>address;}
void display(){
cout<<name;
cout<<address;
getSalary();}
Private Member Functions
class Sample{ However, the function read() can
int m; be called by the member
void read(void){ function update()
m=5; Update the value of m.
}
public: void sample::update(void)
void update(void); {
void write(void); read(); //simple call, no
}; object required.
Sample s1; }
[Link](); //will not work, because
object can not access private
members;
Scope Resolution Operator
• Member functions defined outside class using Binary
scope resolution operator (::)
• “Ties” member name to class name
• Uniquely identify functions of particular class
• Different classes can have member functions with same
name
• Format for defining member functions

ReturnType ClassName::MemberFunctionName( )
{
//function body
}
Scope Resolution Operator
class Employee{
float Employee::getSalary()
private:
{
string name;
return salary;
float salary;
}
float getSalary();
void Employee::setData()
public:
{
string address;
cin>>name;
void setData();
cin>>address;
void display();
}
};
void Employee::display()
{
cout<<name;
cout<<address;
}
Scope Resolution Operator
class Circle{
private:
double radius;
public:
Circle() { radius = 0.0;}
Circle(int r);
void setRadius(double r){radius = r;}
double getDiameter(){ return radius *2;}
double getArea();
double getCircumference();
};
Circle::Circle(int r){
radius = r;
}
double Circle::getArea(){
return radius * radius * (22.0/7);
}
double Circle:: getCircumference(){
return 2 * radius * (22.0/7);
}
Scope Resolution Operator
To access a global variable when there is a local variable
with same name
#include<iostream>
using namespace std;

int x; // Global x

int main() {
int x = 10; // Local x
cout << "Value of global x is " << ::x;
cout << "\nValue of local x is " << x;
return 0;
}
Scope Resolution Operator
3. To access a class’s static variables
class Test {
static int x;
public:
static int y;
void func(int x) { // We can access class's static variable even if there is a local variable
cout << "Value of static x is " << Test::x;
cout << "\nValue of local x is " << x;
}
};
int Test::x = 1; // In C++, static members must be explicitly defined like this
int Test::y = 2;
int main() {
Test obj;
int x = 3 ;
[Link](x);
cout << "\nTest::y = " << Test::y;
return 0;
}
Scope Resolution Operator

4. In case of multiple Inheritance

If same variable name exists in two ancestor classes, we can


use scope resolution operator to distinguish.

Detail would be discussed in inheritance………………


Scope Resolution Operator
5. Including In-built libraries.

#include<iostream>
using std::cout;
using std::cin;
using std::endl;

#include<string>
using std::string;
using std::getline;
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