Chapter 4 Chapter 4
Session Objectives
Classes and Objects Class Declaration Class Members Data Constructors Data Constructors Destructors Member Functions
Class Member Visibility Private, Public,Protected
Session objectives
Default Constructor
Constructor with arguments
Constructor with default arguments
Dynamic constructor
Dynamic constructor
Copy Constructor
Overloaded Constructor
Objects as Function arguments
Member functions defined outside the class. Objects as arguments
Session Objectives
Returning objects from functions Class array as class member data Array of objects
String as class member.
Accessing Class Members
To access the members of the class we make use of the
DOT(.) Operator. Syntax: Object-name.function-name(actual arguments) Object-name.function-name(actual arguments) Example: x.getData(100,75.5);
Defining Member Functions
Member functions can be defined in two places.
Outside the Class definition Inside the class definition
Outside the Class Definition Outside the Class Definition
Member functions that are declared inside the class
have to e defined separately outside the class.
Their definition is much like normal functions. They should have a function header and a body.
Outside the Class Definition
An important difference between a member function and a
normal function is that a member function incorporates a membership ‘identity label’ in the header.
This label tells the compiler which class the function This label tells the compiler which class the function
belongs to.
The general form of a member-function definition is :
return-type class-name:: function-name(argument declaration) {
Function-Body }
Outside the Class Definition
The membership label class-name:: tells the compiler
that the function-name belongs to the class-name .
That is the scope of the function is restricted to the
class name specified in the header line. class name specified in the header line.
The symbol :: is called the scope resolution operator.
Example:
void sum::getdata(int a,int b) {
sum=a+b; }
Defining Member Functions
The member functions have some special
characteristics that are often used in the program development:
Several different classes can use the same function Several different classes can use the same function
name. The membership label will resolve their scope.
Member functions can access the private data of the
class. A non-member function cannot do so.
A member function can call another member function
Inside the class definition
Another method of
declaring a member
function is to replace the function definition by the actual definition.
Example
class sum{ int a,b; public:
the actual definition. public:
void getdata(int x,int y) {
a=x; b=y }
Making Outside function Inline
A member function outside the class definition can still be
made inline by using the qualifier inline.
Example:
class item{ :::::::::::::::::: :::::::::::::::::: public:
void getdata(int a,int b); };
inline void item::getdata(int a,int b) {
:::::::::::::::::::::: }
Nesting Member Functions
As discussed earlier a member function of a class can
be called by an object of that class using the dot operator.
However there is an exception to this. However there is an exception to this.
A member function can be called by using its name
inside another member function of that same class.
This is known as nesting of member functions
Arrays within a class
Arrays can be used as member variables in a class For example: class arraydemo{ int a[20]; int a[20]; public: void getval(){ for(int i=0;i<n;i++) cin>>a[i]; } }; Program: CH4PG8.cpp
Memory Allocation for Objects
Member variable 1 Member variable 2 Member function1 Class Member variable 1 Member variable 2 Member variable 1 Member variable 2 Member variable 1 Member variable 2 Member function1 Member function2Static Data Members
Whenever a object is created a new copy of data
variables is created for that object.
This means that each objects works on its own copy of
data variables. data variables.
C++ uses another type of variable declaration called as
static.
A data member of class can be qualified as static The static member variable has certain special
Static Data Members
These are:
It is initialized to zero when the first object of its class is
created. No other initialization is permitted.
Only one copy of that member is created for the entire Only one copy of that member is created for the entire
class and is shared by all the objects of that class, no matter how many objects are created.
It is visible only within the class, but its lifetime is the
Static Data Members
PROGRAM:CH4PG9.CPP
Note that the type and scope of each static member
int item :: count ;
Note that the type and scope of each static member
variable must be defined outside the class definition.
This is necessary because the static data members are
stored separately rather than as a part of an object.
Since they are associated with class itself rather than
Static Data Members
number number number
Object A Object B Object C
count
Static Member Functions
Like static member variables, C++ has static member
functions.
A member function that is declared as static has
following properties: following properties:
A static function can have access to only other static
members (functions or variables) declared in the same class.
A static member function can be called using the class
name (instead of its object)
Syntax: class-name::function-name; Program: Ch4Pg10.cpp
Array of Objects
Arrays can be of any data type including class and
struct.
Such variables are called array of objects. Syntax; Syntax; class-name object-name[size]; Example: employee manager[20]; Program :CH4PG11.cpp
Objects as function arguments
Like any other data type, an object may be used as a
function argument.
This can be done in two ways:
A copy of the entire object is passed to the function. A copy of the entire object is passed to the function.
Only address of the object is transferred to the function.
Friend Functions
It is possible to grant a non-member function access to
the private members of a class by using a friend.
A friend function has access to all private and
protected members of the class for which it is a friend. protected members of the class for which it is a friend.
To declare a friend function, include its prototype
within the class, preceding it with the keyword friend.
Syntax:
friend type fn-name(arg-list){ //function body
Friend Function
In this example, the sum() function is not a member of
myclass.
However, it still has full access to its private members. Also, notice that sum() is called without the use of the Also, notice that sum() is called without the use of the
dot operator.
Because it is not a member function, it does not need
to be (indeed,it may not be) qualified with an object's name.
Friend Function
A friend function possesses certain special
characteristics:
It is not in the scope of the class to which it has been
declared as friend.
Since it is not in the scope of the class, it cannot be
called using the object of that class.
It can be invoked like normal function without the help
of any object.
It can be declared in public or private part of the class
without affecting its meaning.
A function friendly to two classes
Program:CH4PG14.cpp
The function max() has arguments from both XYZ and
ABC.
When the function max() is declared as a friend When the function max() is declared as a friend
function in XYZ for the first time, the compiler will not acknowledge the presence of ABC unless its name is declared in the beginning as
class ABC;
Friend Classes
It is possible for one class to be a friend of another
class.
When this is the case, the friend class and all of its
member functions have access to the private members member functions have access to the private members defined within the other class.
For example,
class Y {
friend class X; };
Class
It is possible to define short functions completely
within a class declaration.
When a function is defined inside a class declaration,
it is automatically made into an inline function (if it is automatically made into an inline function (if possible).
It is not necessary (but not an error) to precede its
declaration with the inline keyword.
Returning Objects
A Function can not only receive objects as arguments
but also can return them .
The following program demonstrates this concept.
Program: CH4PG17.cpp
Structures and Classes Are Related
A class is syntactically similar to a struct. But the
relationship between a class and a struct is closer than you may at first think.
In C++, the role of the structure was expanded,
In C++, the role of the structure was expanded,
making it an alternative way to specify a class.
In fact, the only difference between a class and a struct
is that by default all members are public in a struct and private in a class.
Unions and Classes Are Related
Like a structure, a union may also be used to define a
class.
In C++, unions may contain both member functions
and variables. and variables.
They may also include constructor and destructor
functions.
A union in C++ retains all of its C-like features, the
most important being that all data elements share the same location in memory.
Constructors
A constructor is a special member function whose task is
to initilize the object of its class.
The constructor is invoked whenever the object of the class
is created. Syntax: Example: Syntax: class class-name{ public: class-name() //constructor { } }; Example: class integer{ int x,y; public: integer() { x=0; y=0; } };
Constructors
Characteristics of constructors;
They should be declared in public section.
They have no return type not even void and therefore
they do not return values. they do not return values.
They cannot be inherited , though derived class
constructors can call base class constructor.
Constructors cannot be virtual.
Types of constructors
There are three types of constructors in C++:
Default Constructors
Parameterized Constructors Copy constructors
Copy constructors
Default Constructors:
A default constructor is a constructor which does not
Parameterized Constructors
A constructor which accepts arguments is called as
parameterized constructor.
Syntax:
class-name :: class-name(argument-list) class-name :: class-name(argument-list) {
//body of the constructor } Example: class integer{ int x,y; public: integer(int a, int b) { x=a; y=b }
Parameterized Constructors
An parameterized constructor can be called using two
ways
By calling the constructor explicitly By calling the constructor implicitly By calling the constructor implicitly
Examples:
integer i=integer(10,20); //explicit call integer i(10,20); //implicit call
Constructor Overloading
A class can have one or more than one constructors
defined in a class with different number of arguments or different type of arguments.
This is called constructor overloading. This is called constructor overloading.
The compiler recognizes the constructor depending
upon the number of arguments or type of arguments.
Copy Constructor
A Constructor which accepts an copy of its own class as
an argument is called an copy constructor.
Syntax:
class-name:: class-name(class-name &obj) class-name:: class-name(class-name &obj)
{
//constructor body }
A copy constructor is used to declare and initialize an
Dynamic Constructor
The constructor can also be used to allocate memory
while creating objects.
This ensures the system to allocate the right amount of
memory for each object when the objects are not of memory for each object when the objects are not of the same size, thus resulting in the saving of memory.
Allocation of memory to objects at the time of their
Arguments
It is possible to define constructors with default
arguments.
For example:
Complex( float real, float img=0); Complex( float real, float img=0);
The argument img=0. Then the statement
Complex(5.0);
Assigns 5.0 to real and 0 to img (by default) variables.
Complex(2.0,3.0);
Near Gomatesh School, Hindwadi Belgaum-11, Phone no: 4200864,2481703 Get Certified in •Microsoft Office 2010 •Tally 9.2 •.NET 2008 •J2EE •CORE JAVA
Around 1800 students
developed software projects
and scored the top scores in
exams.
One stop shop for
•CORE JAVA •C PROGRAMMING •OOPS WITH C++ •JOOMLA •WORDPRESS •PHP AND MYSQL •SQLSERVER
•UNIX AND LINUX
•ANDRIOD APPS
One stop shop for
Software Projects
Website Development
Technology Training I.T Research