You are on page 1of 18

Classes and Objects

Classes
• Classes are created using the keyword class.

• A class declaration defines a new type that links code and


data. This new type is then used to declare objects of that
class.
• An object is an instance of a class.

• A class declaration is similar syntactically to a structure.


Classes
classclass-name

private data and functions

access-specifier:

data and functions

access-specifier:

data and functions

// ...

access-specifier:

data and functions

} object-list;
Classes
The object-list is optional. If present, it declares objects of
the class. Here, access-specifier is one of these three C++
keywords:
public
private
protected
• By default, functions and data declared within a class are
private to that class and may be accessed only by other
members of the class.
• The public access specifier allows functions or data to be
accessible to other parts of your program.
• The protected access specifier is needed only when
inheritance is involved.
Classes
• You may change access specifications as often as you like
within a class declaration.
• Functions that are declared within a class are called
member functions.
• Member functions may access any element of the class of
which they are a part.
• This includes all private elements. Variables that are
elements of a class are called member variables or data
members. Collectively, any element of a class can be
referred to as a member of that class.
 Class Methods
• Methods are functions that belongs to the class.
• There are two ways to define functions that belongs
to a class:
Inside class definition
Outside class definition

Note: You access methods just like you access


attributes; by creating an object of the class and by
using the dot syntax (.)
 Class Methods
To define a function outside the class definition, you
have to declare it inside the class and then define it
outside of the class. This is done by specifying the name
of the class, followed the scope resolution : :
operator, followed by the name of the function.
Classes
• Write a class having two public variables and one
member function which will return the area of the
rectangle.
• Write a C++ program to create a class for student to
get and print details of N students.
• Write a C++ program to read time in seconds and
convert in time format (HH:MM:SS) using class
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,public and
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.
• friends can be useful when you are overloading certain
types of operators.
• Friend functions make the creation of some types of I/O
functions easier .
Friend Functions
• Friend functions may be desirable is that in some cases,
two or more classes may contain members that are
interrelated relative to other parts of your program.
• A friend of one class may be a member of another.
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
defined within the other class.
Friend Classes
• In this program, all member functions of class B will be
friend functions of class A . Thus, any member function
of class B can access the private and protected data of
class A. But, member functions of class A cannot access
the data of class B.

• friend relation in C++ is only granted, not taken.

• classes typically require several frequently executed


interface functions (which provide access to private data),
the efficiency of these functions is of critical concern.
Friend Classes
• Each time a function is called, a significant amount of
overhead is generated by the calling and return
mechanism.
• Arguments are pushed onto the stack and various
registers are saved when a function is called, and then
restored when the function returns. The trouble is that
these instructions take time.
• when a function is expanded in line, none of those
operations occur.
• Although expanding function calls in line can produce
faster run times, it can also result in larger code size
because of duplicated code.
Friend Classes
• For this reason, it is best to inline only very small
functions. Further, it is also a good idea to inline only
those functions that will have significant impact on the
performance of your program.
• Inline is actually just a request, not a command, to the
compiler. The compiler can choose to ignore it.
inline return type function-name(parameters)
{
//function code
}
Constructors
• A constructor function is a special function that is a
member of a class and has the same name as that class.
• Constructor functions cannot return values and, thus,
have no return type.
• Most constructor functions will not output or input
anything. They will simply perform various
initializations.
• An object's constructor is automatically called when the
object is created. This means that it is called when the
object's declaration is executed.
Parameterized Constructors
• It is possible to pass arguments to constructor functions.
Typically, these arguments help initialize an object when
it is created.
• To create a parameterized constructor, simply add
parameters to it the way you would to any other function.
• It is used to initialize the various data elements of
different objects with different values when they are
created.
• It is used to overload constructors.
Destructors
• The complement of the constructor is the destructor. In
many circumstances, an object will need to perform some
action or actions when it is destroyed.
• Local objects are created when their block is entered, and
destroyed when the block is left.
• Global objects are destroyed when the program
terminates.
• When an object is destroyed, its destructor (if it has one)
is automatically called.
• Destructor handles deactivation events. The destructor
has the same name as the constructor, but it is preceded
by a ~.

You might also like