Classes and Objects Classes and Objects

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 46

Kapil Sehgal PGT Computer.

Science Ankleshwar Gujarat

Chapter 4
Classes and Objects
n Classes
n Data Hiding and Encapsulation
n Function in a Class
n Using Objects
n Static Class members
Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat

Classes
n Class represents a group of Similar objects
n A class is a way to bind the data describing an
entity and its associated function together.
n Consider an account having characteristics
n Account no., type, balance
n Its associated operations are DEPOSITE and
WITHDRAWAL.
Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat

Class Definition (Construct)


n class account
n {
n int acno;
n int type;
n float balance;
n float deposit( float amt)
n {
n balance = balance + amt;
n }
n float withdrawal (float amt)
n {
n balance = balance – amt;
n }
n };
Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat

Why class are needed ?


n In the software field of Information Technology
Industry, a need was felt to create software to real
world object. Real world objects certainly have data-
data-
type properties such as account no. balance etc. But
real world object also can also do certain things like
deposit and withdrawal. Now to combine all these
properties of real world objects, one needed a way
out. The structure of C could provide a half way help
by binding the data type properties but still the
operation association left out.
Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat

Declaration of Class
n The declaration of class involves declaration of
its four associated attributes:
n Data members are the data type properties that
describe the characteristics of a class. There
may be zero or more data members in a class
n Member function are the set of operations that
may be applied to objects of that class. There
may be zero or more member function for a
class. They are referred as class interface.
Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat
n Continue…
n Program Access Level that control access to
member from within the program. These access
levels are PRIVATE, PROTECTED and
PUBLIC. Depending upon the access level of a
class member access to it is allowed or denied.
n Class tagname that serves as a type specifier for
the class using which objects of this class type
can be created.
Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat
Structure of Class
n class classname
n {
n private:
n ………… private members and function visible only inside the
class
n protected:
n …………. protected members and function visible class and his
child classes
n public:
n ………….. Visible in any where in the program
n }
Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat
Class Method Definition
n Methods (Member Function) can be defined
from two ways
n Inside the class
n Outside the class.
Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat
Inside the class - Method Definition
n Methods of class often define in public section.
n class name
n {
n private:
n ….. Data member;
n public:
n return--type method_name (paramete list)
return
n {
n ………….. Function Body
n }
n };
Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat
Outside the class - Method
Definition
n return-type class_name :: method_name (Parameter list)
return-
n {
n ……….. Function body
n }
n Here :: pronounced as ‘Scope Resolution Operator’
Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat
Difference between struct and class
n In struct all members are public but in class all
members by default be private.
n In struct there is no methods but class hold
members as well as methods.
n struct can not derived any other struct, but class
can derive any other class.
Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat
Array within the class
n A class can have an array as its member variable. An
array in the class can be private or public data member
of the class.
n class exarray
n {
n int arr[10];
n public:
n int largest();
n int sum();
n };
Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat
Scope of Class and its member
Public members are the members that can
access directly by any function. Whether
member function of the class or non member
function
Private Member are the class members that are
hidden from the outside world. The private
member of the class can be used by the member
function of the class
Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat
Scope Rules and Class
n Global Class
n A class is said to be global class if its definition occurs
outside the bodies of all functions in a program. Which
means that object of this class type can be declared
from any where in the program.
n Local Class
n A class is said to be local if its definition occurs inside a
function body. Which means that object of this class
can be declared only within the function.
Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat
Example
n #include<iostream.h>
n void main()
n { // main started
n class y
n { // class started
n ………
n }; // class end
n } // main end
Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat
Scope for Objects – Global Object
n An object is said to be global Object if it is
declared outside all the function bodies and it
means that this object is globally available to all
function in the program. This object can be used
any where in the program.
Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat
Local Object
n An object is said to be local if it is declared
within a function, which means that this object
is locally available to the function that declares it
and it cannot be used outside the function
declaring it.
Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat
Example
n #include<iostream.h>
n class X
n {
n …..
n };
n X obj1,obj2; // // Global object;
n void main()
n {
n X obj3,obj4; //Local object
n }
Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat

Scope of Private and Protected member

n The PRIVATE and PROTECTED members of


a class have class scope. It means these can be
accessed only by the member function of the
same class. These members can not be accessed
directly by using the object names.
n In other words nowhere except the bodies of
member function can be names of private and
protected member appear.
Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat
Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat
Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat
Scope of Public members
n The scope of the public members depends upon
the object being used for referencing them. If
the referencing object is a global object, then the
scope public members is local.
Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat

#include<iosream.h void main() void fun1() {


class x { private : int a; { x obj2; x obj3;
void fc() { obj1.i=10; obj1.i=12; Valid
cout<<a; } obj1.fcc(); obj1.fcc(); Valid
public: int i; obj2.i=20; obj2.i=25 Invalid
void fcc() { obj2.fcc(); } obj2.fcc(); Invalid
cout<<2*i; obj3.i=30; Valid
A=13; obj3.fcc(); Valid
fc();}}; }
x obj1;
Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat
Types of Class Function
n Accessor Function
n Mutator Function
n Manager Function
Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat
Accessor Function
n These Function are those which can not change
the data members value, but they simply use for
the retrieving the value of data members;
n It means, Accessor function are used to read
values of private data members of a class which
are directly not accessible in non member
function.
Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat

class student int getrollno()


{ {
int rollno; return(rollno);
float marks; }
char name[25]; float getmarks()
char grade; {
public: return(marks);
void readdata() { } }
void printdata() { } };
Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat
Mutator Function
n This type of function are used to manipulate the
value of private data members. Generally most
of the member function are such type.
Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat

void calculate()
class student {
{ if marks>=90
int rollno; grade='A';
else
float marks;
if marks>=75
char name[25]; grade='B';
char grade; else
public: if marks>=60
grade='C';
void readdata() { } else
void printdata() { } grade='D';
}
Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat
Manager Function
n These are the member function with specific
type. This types of function are also
‘CONSTRUCTOR’ and ‘DESTRUCTOR’.
n These function are used to initialize and
destroying the objects.
Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat
Nested Class
n A class is declared within another class. A class
is declared within another class is called nested
class.
n The outer class is class is called enclosing class
and inner class is called the nested class.
Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat

Member Access Control of Nested Class


n The member function of nested classes have no special
access to members of an enclosing class. They obey the
usual access rule and the same is true for enclosing class
for the access of nested class members
n Nested class can access public member of enclosing by
own object and similarly enclosing class can access
inner class public member by own object.
n Private and Protected member is not available for other
class.
Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat
Inline Function
n The inline function are a C++ enhancement
designed to speed up programs. The coding of
normal function and inline function is similar
except that inline functions definition starts with
the keyword “inline”.
n The distinction between normal function and
inline function is the different compilation
process for them.
Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat
Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat

Original Program Compiled Program


void main void main
{ {
... ...
.... ....
square(3); {
... cout<<3*3;
.... }
square(3); ...
... ....
.... {
square(3); cout<<3*3;
} }
void square(int i) ...
{ ....
cout<<i*i; {
} cout<<3*3;
}
}
Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat
When we make function inline
n The function should be inline only when they
are small
n The function which does not contains return
value, loop, switch and goto statement.
n The function which does not contains static
variables.
n The function should not recursive.
Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat
Constant Member Function
n If a member function of a class does not alter
any data in the class. This member function may
be declared as constant member function using
the keyword const
n Example.
n int max (int, int) const;
n void prn(void) const;
Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat
Use of :: Operator
n int x,y;
n class A
n { public:
n int x;
n void f(int x)
n {
n x=i;
n ::x=i;
n y=i;
n }
n };
Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat
Array of Object
n C++ Supports arrays of any data type including
class type. An array having class type elements is
known as “ Array of Object “.
n Array of Object is declared after the class
definition is over and it is defined in the same
way as other array is defined.
Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat
Object as Function Arguments
n Just like any other data types of C++, an object
is also passed as function argument.
n An Object can also be passed as
n Call by Value and Call by Reference
n An Object can be passed to any member
function , non member function and any friend
function.
Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat
Function Returning Object
n An object can not only be passed to the function but a function
can also return an object.
n In other words, A function can also be a class type.
n student total (student);
n distance add (distance d, distance e)
n {
n distance k;
n ---
n ---
n Return(k);
n }
Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat
Static Data Members
n Static data members of a class just like a global
variable for its class. The static data members are
stores value common to the entire class.
Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat

Difference between static and simple


data members

n There is only one copy of the data members of a


class is maintained for the entire class, which is
shared by all the object of the class.
n It is visible only within the class. However its life
time (the time for which it remain in memory) is
the entire class.
Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat

How to make static data member


n Declaration within the class
n class X { static int count;
n ………};
n Definition outside the class
n Type ClassName :: DataMember = value;
n int X :: count = 0;
n A static data member must be defined outside
the the class definition.
Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat

Initialization of const Static data


Members
n const data members of standard data type now
can be initialized inside the class. Initialization is
also a type of definition, so no further definition
is required outside the class.
n class demo {
n static const int amount =400;
n static const float val =50;
n };
Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat
Static Member Function
n A static member function that accesses only the
static data members of a class may be declared
as static. This can be done by putting the
keyword “static” before the function declaration
in the class definition.
n class demo { static int a;
n public:
n static void show() { ….. …. }
n };
Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat

Difference between static member


function and other member function
n A static member function can access only static
data members of the same class, But other
member function can access both type of data
member..
n A static member function can invoked by using
the class name instead of object of class like
className :: staticFunctionName()

You might also like