KEMBAR78
DownloadClassSessionFile 8 | PDF | Pointer (Computer Programming) | Class (Computer Programming)
0% found this document useful (0 votes)
32 views70 pages

DownloadClassSessionFile 8

The document discusses classes in C++. It begins by explaining that classes represent the basic building blocks of C++ and combine data and functions. Classes allow data and related functions to be permanently associated. Inheritance is described as allowing a class to inherit behaviors from another class and redefine functions as needed. The document then covers declaring classes using the class keyword, and describes public, private, and protected access specifiers. Constructors and destructors are introduced as automatic functions for initializing and cleaning up class objects. Pointers are discussed as allowing dynamic memory allocation.

Uploaded by

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

DownloadClassSessionFile 8

The document discusses classes in C++. It begins by explaining that classes represent the basic building blocks of C++ and combine data and functions. Classes allow data and related functions to be permanently associated. Inheritance is described as allowing a class to inherit behaviors from another class and redefine functions as needed. The document then covers declaring classes using the class keyword, and describes public, private, and protected access specifiers. Constructors and destructors are introduced as automatic functions for initializing and cleaning up class objects. Pointers are discussed as allowing dynamic memory allocation.

Uploaded by

snn8hddmmx
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 70

Chapter 3

CLASSES
2.1 Introduction Cont.

11/27/2017 2
2.1 Introduction Cont.
• The most important feature differentiating C++
from C is the concept of Class both at the
syntactic and conceptual level.
• A Class represents the basic building blocks of
C++.
• It is simply an extension of the C structure.
• However a Class allows us to create a
structure , and then permanently associate all
related functions to the data of the structure.
11/27/2017 3
2.1 Introduction Cont.

Data + Functions = Object

11/27/2017 4
5
2.1 Introduction Cont.

11/27/2017 6
2.1 Introduction Cont.

11/27/2017 7
2.1 Introduction Cont.
• Classes are a collection of Variables and
functions that operate on those variables.
• The variables in a Class definition are called
Data Members, whereas Class functions are
called Member Functions.

Data Members are Width and


Length.

Class Rectangle The Member function will be a


function to calculate the area of
11/27/2017
the Rectangle. 8
2.1 Introduction Cont.
Now if we are to define a new class of a Square, then
we will have similar data members with the special
case having Width=Length, a member function to
calculate the area of a square is also needed.
So instead of defining the Class square from scratch we
can think of the square as a special case of
rectangle , hence making use of class of rectangle by
inheriting its behavior and redefining the area
function to work for the class of squares.
Thus this process is called Inheritance.
11/27/2017 9
2.2 Declaration of Classes
• Classes are declared using the keyword class.
• Its functionality is similar to that of the
keyword struct.
• Structure can have only data as members.
• Classes can have data and functions as
members.

11/27/2017 10
2.2 Declaration of Classes Cont.
class class_name {
public:
Public_Data_Members;
Public_Data_Functions;
private:
Private _Data_Members;
Private _Data_Functions;
protected:
Protected _Data_Members;
Protected _Data_Functions;
} object_name;
11/27/2017 11
2.2 Declaration of Classes Cont.
• Class_name is a name for the Class (User
defined type).
• The optional field object_name refers to on or
more valid object identifiers.
• public , private and protected represents
different section labels in the declaration of a
class in C++ that represents member access
specifiers.

11/27/2017 12
2.2 Declaration of Classes Cont.
• In each section different data members and
member functions could be included.
• If no section label mentioned , then the
Private label is default.
• The order of the labels is not important and
they can be ordered in any way possible.
• These sections may also be repeated many
times inside the a Class declaration.

11/27/2017 13
2.2 Declaration of Classes Cont.
• The public section represents the Class
interface to the outside world.
• Members of the public section are accessible
form anywhere the Class is visible in the
program.
• The private section on the other hand
represents that part of the Class that can be
accessed only by member functions and
friends of that Class.
11/27/2017 14
2.2 Declaration of Classes Cont.
• The protected section represents that part of
the Class that includes parts that are
accessible by member functions , Friends of
the Class , as well as member functions and
friends of the derived Classes.

11/27/2017 15
Example: on how to model a house using a
Class

11/27/2017 16
Example of Creating a Class for rectangles

- As the number of member


functions in a class
increases, the related
code would become so
messy and the class
definition would become
crowded. To overcome
the problem, the Scope
resolution operator “::”
can be used.
11/27/2017 17
The header :
int rectangle::area()
would indicate a
member function
called area of a
class called
rectangle and has
no parameters and
returns an integer.

11/27/2017 18
• The compiler would have no problem
accessing and executing member functions
and data members declared inside the public
section.
Data members and data
functions declared under
private section cannot be
easily accessed and used
outside class

11/27/2017 19
• Private members can be accessed through
member functions.

11/27/2017 20
• Private functions are usually helper functions
that are not needed outside the object.
• To access the data inside the private section
we should declare a function of friend type.

11/27/2017 21
Length and width data
members and the
function area under
private function accessed
by access friend function.

11/27/2017 22
Rectangle example
with two objects.

11/27/2017 23
2.3 – Class Constructors and Destructors.

• For each of the instances of the class rectangle


we should perform an initialization.
• It gets a bit annoying declaring the value of
each data member of the class using the
method shown below:

11/27/2017 24
2.3 – Class Constructors and Destructors.
Cont.
❖One way to overcome the issue is to use what is
known as a Class Constructor.
❖A Class constructor is a function that is executed
automatically whenever a new instance if the given
class is declared.
❖The aim of Class constructor is to perform any
initialization related to the class instances via
passing of some parameter values as initial values
and allocate proper memory locations for that
object.
11/27/2017 25
2.3 – Class Constructors and Destructors.
Cont.
❖A class constructor must have the same name
as that of the associated class.
❖A class constructor has no return type not
even void.
❖A class constructor can be overloaded.

11/27/2017 26
2.3 – Class Constructors and Destructors.
Cont.

11/27/2017 27
2.3 – Class Constructors and Destructors.
Cont.

11/27/2017 28
2.3 – Class Constructors and Destructors.
Cont.
• After the object become out of scope or no
longer is needed, it is necessary to perform
what is known as house keeping to reclaim the
memory locations that were allocated for that
object.
• To this , another function called a class
Destructor fulfills this opposite functionality.

11/27/2017 29
2.3 – Class Constructors and Destructors.
Cont.
• A class destructor is also called automatically
whenever an object is released and no longer is
needed.
• The destructor must have the same name as
the associated class preceded with a tilde (~) as
a prefix.
• A class destructor also has no return type.
• It takes no parameters.
• It cannot be overloaded.
11/27/2017 30
2.3 – Class Constructors and Destructors.
Cont.

11/27/2017 31
2.3 – Class Constructors and Destructors.
Cont.
• For class destructors we need objects to be
pointers in order to be able delete them.

Pointers???

11/27/2017 32
2.3.1 – Pointers
• A normal variable is a location in memory that
can hold a value.
• As a program is executing all variables are
stored in memory. Each at its own unique
address or location.

11/27/2017 33
2.3.1 – Pointers (cont.)
• For example when the variable (i) is declared as an
integer , four bytes of memory are set-aside for it.
• A certain amount of memory will be reserved for
each normal variable. Will be reserved for the
variable as long as the program is active.
• The size of normal variable is fixed at compilation
time.
• The structure of a normal variable cannot be
changed during run-time.

11/27/2017 34
2.3.1 – Pointers (cont.)
Consider for example the following array
declaration :
int somearray[2000];
According to this declaration 2000 memory
locations of type int will be reserved under
the name somearray, and these locations will
be reserved during the entire course of
program execution .

11/27/2017 35
2.3.1 – Pointers (cont.)
• This may create two problems:
1- Bad memory utilization.
2- Shortage of memory.

11/27/2017 36
2.3.1 – Pointers (cont.)
• C++ solves these problems by using dynamic
variables.
• Memory for dynamic variables is allocated when
needed and de-allocated when not needed during
the execution of a program.
• The memory is not wasted.
• C++ used pointers to create Dynamic Data
Structures .
• A pointer is a special variable that contains a
memory address rather than a data value.
11/27/2017 37
2.3.1 – Pointers (cont.)
• So a pointer is a variable that points to
another variable by holding the address of the
variable.

11/27/2017 38
2.3.1 – Pointers (cont.)
Declaration of pointers

int y;
Y=5;
Int* ypoint; // this is a pointer
ypoint=&y; // Assigning the address of y variable to
ypoint pointer.

11/27/2017 39
2.3.1 – Pointers (cont.)
• The variable ypoint is declared to be a pointer
of type integer.

0xff3423

ypoint y contains
Contains 5 as a value
0xff3423
11/27/2017 40
2.3.1 – Pointers (cont.)
Examples of pointer declarations:

char *s, *t;


float *fp;

Like any other variables pointers


should be declared then initialized.
11/27/2017 41
2.3.1 – Pointers (cont.)
• To get the value that is stored in the memory
location pointed by a pointer it is necessary to
deference that pointer.

11/27/2017 42
2.3.1 – Pointers (cont.)

11/27/2017 43
2.3.1 – Pointers (cont.)

11/27/2017 44
2.3.1 – Pointers (cont.)
• The new operator allocates memory of a
proper type needed for the dynamic variable.
• The delete operator is used to deallocate the
area reserved for the dynamic variable.

• ACTIVITY: Create a dynamic array having 100


elements.

11/27/2017 45
2.3 – Class Constructors and Destructors.
Cont.

11/27/2017 46
2.3 – Class Constructors and Destructors.
Cont.

11/27/2017 47
2.3 – Class Constructors and Destructors.
Cont.

11/27/2017 48
2.4 Overloading Constructors
In C++, We can have more than one constructor
in a class with same name, as long as each has
a different list of arguments. This concept is
known as Constructor Overloading and is
quite similar to function overloading.
• Overloaded constructors essentially have the
same name (name of the class) and different
number of arguments.

11/27/2017 49
2.4 Overloading Constructors
• A constructor is called depending upon the
number and type of arguments passed.
• While creating the object, arguments must be
passed to let compiler know, which
constructor needs to be called.

11/27/2017 50
11/27/2017 51
2.4 Overloading Constructors

11/27/2017 52
11/27/2017 53
2.4 Overloading Constructors

11/27/2017 54
11/27/2017 55
2.5 Templates
Function templates are special functions that
can operate with generic types. This allows us
to create a function template whose
functionality can be adapted to more than one
type or class without repeating the entire
code for each type.

11/27/2017 56
2.5 Templates
In C++ this can be achieved using template
parameters. A template parameter is a special
kind of parameter that can be used to pass a
type as argument: just like regular function
parameters can be used to pass values to a
function, template parameters allow to pass
also types to a function. These function
templates can use these parameters as if they
were any other regular type.
11/27/2017 57
2.5 Templates
The format for declaring function templates with
type parameters is:

template <class identifier> function_declaration;

template <typename identifier> function_declaration;

11/27/2017 58
2.5 Templates
The only difference between both prototypes
is the use of either the keyword class or the
keyword typename. Its use is indistinct, since
both expressions have exactly the same
meaning and behave exactly the same way.

For example, to create a template function


that returns the greater one of two objects we
could use:
11/27/2017 59
2.5 Templates

Here we have created a template function with myType as


its template parameter. This template parameter
represents a type that has not yet been specified, but that
can be used in the template function as if it were a regular
type. As you can see, the function template GetMax returns
the greater of two parameters of this still-undefined type.

11/27/2017 60
2.5 Templates
To use this function template we use the
following format for the function call:

function_name <type> (parameters);

For example, to call GetMax to compare two


integer values of type int we can write:

11/27/2017 61
2.5 Templates
When the compiler encounters this call to a
template function, it uses the template to
automatically generate a function replacing
each appearance of myType by the type
passed as the actual template parameter
(int in this case) and then calls it. This process
is automatically performed by the compiler
and is invisible to the programmer.

11/27/2017 62
2.5 Templates
Here is the entire example:

11/27/2017 63
2.5 Templates
Notice how in this case, we called our function
template GetMax() without explicitly specifying the
type between angle-brackets <>. The compiler
automatically determines what type is needed on each
call.

Because our template function includes only one


template parameter (class T) and the function
template itself accepts two parameters, both of
this T type, we cannot call our function template with
two objects of different types as arguments:
11/27/2017 64
2.5 Templates
We also have the possibility to write class
templates, so that a class can have members
that use template parameters as types. For
example:

11/27/2017 65
2.5 Templates
The class that we have just defined serves to
store two elements of any valid type. For
example, if we wanted to declare an object of
this class to store two integer values of
type int with the values 115 and 36 we would
write:

11/27/2017 66
2.5 Templates
this same class would also be used to create an
object to store any other type:

11/27/2017 67
2.5 Templates
The only member function in the previous class
template has been defined inline within the
class declaration itself. In case that we define a
function member outside the declaration of
the class template, we must always precede
that definition with the template <...> prefix:

11/27/2017 68
2.5 Templates
Notice the syntax of the
definition of member
function getmax:

11/27/2017 69
2.5 Templates
Confused by so many T's? There are three T's in
this declaration: The first one is the template
parameter. The second T refers to the type
returned by the function. And the third T (the
one between angle brackets) is also a
requirement: It specifies that this function's
template parameter is also the class template
parameter.

11/27/2017 70

You might also like