Professional Documents
Culture Documents
Classes and Objects Classes and Objects
Classes and Objects Classes and Objects
Classes and Objects Classes and Objects
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
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
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