2. INTRODUCTION
• C++ class mechanism allows users to define
their own data types that can be used as
conveniently as build in types.
• Classes are often called user defined types.
• Includes defining of class types and creation of
objects of classes.
3. Cont…………
• The class definition introduces both :
• the class data members that define the
internal representation of class
• The class member functions that define the
set of operations that may be applied to
objects of class type.
4. Cont………….
• Information hiding is achieved by declaring
the data members as private whereas the
operations to be performed on class objects
by the program are public.
5. What is class?
• Created using keyword class.
• A class declaration defines a new user defined
data type that links code and data.
• The new data type is then used to declare
objects of that class.
• Class is a logical abstraction but an object has
physical existence.
• Objects is an instance of class.
6. Cont……….
• The class is the cornerstone of C++
– It makes possible encapsulation, data hiding and
inheritance
– A user defined type
– Consists of both data and methods
– Defines properties and behavior of that type
7. Syntax of Class declaration
• Class className
• {
• // body of a class
• }
8. Cont…………….
• The class definition has two parts:
• Class head : composed of keyword class
followed by the class name
• Class body: enclosed by a pair of curly braces.
9. General form of class declaration
• Class className
• {
• Access_specifier_1:
• Members;
• Access_specifier_2:
• Members;
• ……
• }
• objectName;
11. Example of student class
• Class student
• {
• Int rollNo; (data members)
• Char name [25];
• Char addr [35];
• Public:
• Void getdata(); (member functions)
• Void display();
• }
• ;
12. Cont………
• The class data members and member
functions are declared within the body of the
class along with their access levels.
• The class body defined the class member list
and their scope.
• If two classes have members with the same
name, the program will not show any error
since the members refer to different objects.
14. Public
• Public members may be accessed by member
functions of same class and functions outside
the scope of the class (anywhere inside the
program)
15. Private
• Private members may only be accessed by
member functions and friend function of the
class.
• A class that enforces information hiding
declares its data members as private.
16. Protected
• The protected members may be accessed only
by the member functions of its class or by
member functions of its derived class.
17. Cont………..
• The data hiding is achieved by declaring data
members in the private section of the class.
• Since the private members are not accessible
from outside the class, they will be accessed
by the publicly declared member functions of
the same class.
18. Defining a class
class box
{
Public:
Int a,b,c; Data Members
void get()
{
cin>>a>>b>>c;
}
void put() Member Functions
{
cout<<a<<b<<c;
}
};
19. C++
IT 3rd Sem
Object-Oriented Programming:
Class:
class <class-name>
{
access-specifier:
variable declarations
function declarations
access-specifier:
variable declarations
function declarations
access-specifier:
variable declarations
function declarations
};
20. Example
• Class rectangle
• {
• Int length;
• Int width;
• Public:
• Void setvalues (int, int);
• Int area();
• };
21. Cont………..
• It declares a class rectangle. The class contains
four members:
• Two data members length and width of type
integer in the private section (because private
section is default permission)
• Two member functions:
• Setvalues (int, int) and area() in the public
section.
22. C++
IT 3rd Sem
Object-Oriented Programming:
Object:
class <class-name>
{
access-specifier:
variable declarations
function declarations
access-specifier:
variable declarations
function declarations
} ob1; //object creation
void main()
{
<class-name> ob2; //object
creation
23. Objects
A class provides the blueprints for objects, so basically an
object is created from a class. Object can be called as an
instance of a class. We declare objects of a class with exactly
the same sort of declaration that we declare variables of basic
types.
Following statements declare two objects of class Box:
Box Box1;
Box Box2;
24. Cont………..
• The definition of class does not cause any
storage to be allocated. Storage is only
allocated when object of class type is defined.
• The process of creating objects of the class is
called class instantiation.
25. Syntax of defining objects of a class
• Class className objectName
• Class : keyword
• ClassName : user defined class name
• User defined object name
26. Object Operations
All operations that can be applied to basic data types can be
applied to the objects.
E.g.:
• Arithematic
• Relational
• Logical
27. Example
• Class rectangle
• {
• Int length;
• Int width;
• Public:
• // member functions
• };
• The definition
• Rectangle rect;
28. Cont………..
Will allocate the memory space sufficient to
contain the two data members of the rectangle
class.
The name rect refers to that memory location.
Each class object has its own copy of the class
data members.
• An object of a class type also has a lifetime.
29. Data members
• Declared in the same way as the variables are
declared.
• Generally, all data members of a class are
made private to that class.
• If data members are declared in the public
section of the class, they will be accessed
using member access operator, dot (.).
30. Syntax of accessing data members of
class
• objectName . Data member
• ObjectName : user defined object name
• . : member access operator
• Data member: data member of a class.
31. Member functions
• Functions that are declared within a class are
called member functions.
• Works on data members of class.
• Member functions can access any element of
the class of which they belong to.
• The member functions of a class are declared
inside the class body.
32. Syntax of accessing member functions
of a class
• objectName . functionName (Actual
Arguments)
• objectName: user defined object name
• . : member access operator
• functionName: name of the member function
• Actual arguments: arguments list to the
function
33. Cont…………
• Member functions are declared within the
scope of their class. It means member
function is not visible outside the scope of its
class.
34. Member function can be defined in
two ways
• Inside the class
• Outside the class
35. Functions defined inside the class
• Member function of a class can be defined
inside the class declaration.
• Its syntax is similar to a normal function
definition except that it is enclosed within the
body of a class.
36. Example
• The member function in the rectangle class can be defined as follows:
• Class rectangle
• {
• Int length;
• Int width;
• Public:
• Void setvalues(int x, int y)
• {
• Length = x;
• Width = y;
• }
• Int area ()
• {
• return(length * width);
• }
• } ;
37. Cont…….
• In this the class contains two member
functions in the public section:
Setvalues(int, int)
Area()
• They are defined inside the class itself.
38. Functions defined outside the class
• In this method, the prototype of the member
function is declared within the body of the
class and then defined outside the body of the
class.
• Functions defined outside the class have the
same syntax as the normal function, there
should be a mechanism of binding to the class
to which they belong.
39. Cont……………
• This requires a special declaration.
• The name of the member function must be
qualified by the name of its class by using the
scope resolution operator. ( : : )
40. General format of member function
definition
• Class className
• {
• ……
• returnType memberFunction (arguments);
• User defined class name
• }
• ;
41. Member function definition outside
the class
• returnType className :: memberFunction
(arguments)
• {
• // body of the function
• }
42. Object-Oriented Programming
Using C++, Third Edition
42
Implementing Class Functions
• When you construct a class, you create two parts:
– Declaration section: contains the class name,
variables (attributes), and function prototypes
– Implementation section: contains the functions
• Use both the class name and the scope resolution
operator (::) when you implement a class function
44. Object-Oriented Programming
Using C++, Third Edition
44
Using Static Class Members
• When a class field is static, only one memory
location is allocated
– All members of the class share a single storage
location for a static data member of that same class
• When you create a non-static variable within a
function, a new variable is created every time you
call that function
• When you create a static variable, the variable
maintains its memory address and previous value
for the life of the program
45. Static variable
• Static variables are sometimes called class
variables, class fields, or class-wide fields
because they don’t belong to a specific object;
they belong to the class.
• Initialized and allocated storage only once at
the beginning of the program execution.
• No matters how many times they are called
and used in the program.
• Retains its value until the end of the program.
49. This pointer
• When a member function is called, an implicit
argument is automatically passed that is a
pointer to the invoking object (that is, object
on which the function is called). This type of
pointer is called this.
51. Constant keyword
• In c++, constant keyword is used to make
program elements constant. Constant keyword
can be used with:
• Variable
• Pointer
• Function arguments and return types
• Class data member
• Class member function
• objects
52. C++
IT 3rd Sem
Object-Oriented Programming:
Types of Member Functions:
Inline Functions
Nested Functions
Friend Functions
Static Functions
Virtual Functions
53. FRIEND FUNCTION
• The protected and private members cannot be
accessed from outside the same class at which
they are declared.
• Its possible to grant a non member function
access to the private members of a class, by
using a keyword friend.
54. Cont………
• A friend function has access to all private and
protected members of the class for which it is
friend.
55. Syntax of friend function
• Class rectangle
• {
• ……
• …..
• Public:
• ……
• ……
• Friend rectangle duplicate (rectangle)
• }
56. Characteristics of friend function
1. It is not member function of the class.
2. It is like normal external functions.
3. It is not in the scope of the class to which it
has been declared.
4. It can be declared either public or private
section of the class.
5. Usually it passes objects as arguments.
60. Nested class
• A class can be defined within other class, such
a class is called nested class.
• A member of its enclosing class.
• Its definition can occur within a public,
protected or private section of its enclosing
class.
61. Local classes
• A class that can be defined inside a function
body. Such a class is called local class.
• It is only visible in the local scope in which it is
defined.
62. Abstract class
• An abstract class is a class that is designed to
be specifically used as a base class.
• An abstract class contains at least one pure
virtual function.
• You declare a pure virtual function by using
a pure specifier (= 0) in the declaration of a
virtual member function in the class
declaration.
63. Cont………..
• You cannot create an object of an abstract
class type; however, you can use pointers and
references to abstract class types.
• A class that contains at least one pure virtual
function is considered an abstract class.
• Used to provide an interface to its sub classes.
64. Pure virtual functions
• Functions with no definition.
• They start with keyword virtual and ends with
= 0
• Syntax is:
• Virtual void f() = 0;
65. container classes
• A Container class is defined as a class that
gives you the power to store any type of data.
• There are two type of container classes in C++,
namely
• “Simple Container Classes” and
• “Associative Container Classes”.
• An Associative Container class associates a key
to each object to minimize the average access
time.
68. Storage classes
• Used to specify the lifetime and scope of the
variables.
• How storage is allocated for variables and how
variable is treated by complier depending
upon these storage classes.
69. 5 types
1. Local variable
2. Global variable
3. Register variable
4. Extern variable
5. Static variable
70. Namespace
• Container for identifiers.
• Puts the names of its member in a distinct
space so that they don’t conflict with the
names in other namespaces.
71. Syntax
• Its creation is similar to class creation
• Namespace Myspace
• {
• Declarations
• }
• Int main() {}
Will create namespace named Myspace, in
which we put member declarations.
72. Class member function
• A member function of a class is a function that
has its definition or its prototype within the
class definition like any other variable.
73. Class access modifier
• A class member can be defined as public,
private or protected. By default members
would be assumed as private.