0% found this document useful (0 votes)
131 views48 pages

Unit-5 PPT Notes

Object Oriented Techniques document discusses key concepts of object-oriented programming including classes, objects, data encapsulation, and access specifiers. It provides examples of defining a simple class with private data members and public member functions, as well as creating arrays of objects and static class members. The document also covers friend functions which allow non-member functions access to private members of a class.

Uploaded by

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

Unit-5 PPT Notes

Object Oriented Techniques document discusses key concepts of object-oriented programming including classes, objects, data encapsulation, and access specifiers. It provides examples of defining a simple class with private data members and public member functions, as well as creating arrays of objects and static class members. The document also covers friend functions which allow non-member functions access to private members of a class.

Uploaded by

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

Object Oriented Techniques

(unit-5)

by
Surendra Kr.Keshari
Assistant Professor
KIET Group of Institutions
1
Introduction of Class
 An object oriented programming approach is a collection of objects and each object
consists of corresponding data structures and procedures.
 The program is reusable and more maintainable. The important aspect in oop is a
class which has similar syntax that of structure.
 A Class is a way to bind the data and its associated functions together. It allows the
data (and functions) to be hidden, if necessary, from external use.
 Generally, a class specification has two parts:
1. Class declaration
2. Class function definitions
 The class declaration describes the type and scope of its members. The class
function definitions describe how the class functions are implemented.

2
General syntax
class class_name
{
Access specifier :
Variable declarations;
Access specifier :
function declarations;
};
 The class declaration is similar to a struct declaration.
 The keyword class specifies, that what follows is an abstract data of type
class_name.

3
Classes (cont…)
 A class declaration is a logical abstraction that defines a new type.
 It determines what an object of that type will look like.
 An object declaration creates a physical entity of that type.
 That is, an object occupies memory space.
 Each object of a class has its own copy of every variable declared within the class
but they all share the same copy of member functions.

4
Access Control
 Access specifier or access modifiers are the labels that specify type of access given
to members of a class. These are used for data hiding. These are also called as
visibility modes. There are three types of access specifiers
1. Private: If the data members are declared as private access then they cannot be
accessed from other functions outside the class. It can only be accessed by the
functions declared within the class. It is declared by the key word “private” .
2. Public: If the data members are declared public access then they can be accessed
from other functions out side the class. It is declared by the key word “public” .
3. Protected: The access level of protected declaration lies between public and
private. This access specifier is used at the time of inheritance.
Note: If no access specifier is specified then it is treated by default as private.
The default access specifier of structure is public where as that of a class is “private”

5
Data hiding in classes

6
A Simple Class Example:
class Item
{
private:
int number; // variable declaration
int cost; // private by default
public:
void getdata (int a, float b); // function declaration
void putdata(); // using prototype
};

7
Creating objects
 An object in C++ is essentially a block of memory that contains space to store all the
instance variables. Creating an object is also referred to as instantiating an object. To
create objects of type item, use statements such as the following:
Item x; //memory for x is created
creates a variable x of the type item. In C++, the class variables are known as objects.
Therefore, x is called an object of type item.
 We may also declare more than one object in one statement.
Example : Item x, y, z;
 Objects can also be created when a class is defined by placing their names
immediately after the closing brace,
Class Item
{ …………
…………
} x,y,z;
would create the object x, y and z of type item.
8
Accessing class members
 The private data of a class can be accessed only through the member functions of
that class. The main( ) cannot contain statement that access number and cost
directly. The following is the format for calling a member function.
Object_name.functionname (actual argument)
x.getdata(100, 75.5);
Similarly, x.putdata();
would display the values of data members.

9
Defining Member Function:
 Member function can be defined as two places:
1. Outside the class definition
return-type class-name :: function-name(argument declaration)
{
function body
}
2. Inside the class definition
We can define a member function inside the class definition. When a function is
defined inside the a class, it is treated as an inline function. Normally, only
small functions are defined inside the class definition.

10
Defining Member Function
1. Outside the class definition 2. Inside the class definition

11
Arrays of objects
 We can have arrays of variables that are of the type class. Such variables are called
arrays of objects. Consider the following class definition:
class employee
{ char name[30];
float age;
public:
void getdata(void);
void putdata(void);
};
 The identifier employee is a user-defined type and can be used to create objects that
relate to different categories of the employees.
employee manager[3];
employee foreman[15];
The array manager contains three objects(managers),
namely, manager[0], manager[1] and manager[2], of type
employee class.
Similarly, the foreman array contains 15 objects (foreman).
12
Static Data members
 A data member of a class can be qualified as static. Static variables are normally
used to maintain values common to the entire class. A static member variable has
certain special characteristics. 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 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 entire program.

13
14
Static member function
 Like static member variable, we also have static member functions. A member
function that is declared static has the following properties:
 A static function can have access to only other static members (function or
variables) declared in the same class.
 A static member function can be called using the class name (instead of it‟s objects)
as follows:
class-name : : function-name;

15
16
F r i e n d Fu n c t i o n s
 The private members cannot be accessed from outside the class. i.e.… a non member
function cannot have an access to the private data of a class. In C++ a non member
function can access private by making the function friendly to a class.
 A friend function is a function which is declared within a class and is defined outside
the class. It does not require any scope resolution operator for defining . It can access
private members of a class. It is declared by using keyword “friend”

17
Example:
class sample {
int x,y;
public:
sample(int a,int b);
friend int sum(sample s);
};
sample::sample(int a,int b)
{ x=a; y=b; }
int sum(samples s)
{ int sum;
sum=s.x+s.y;
return 0;
}
void main()
{
Sample obj(2,3);
int res=sum(obj);
cout<< “sum=”<<res<<endl;
} 18
Characteristics of Friend Function
 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 a normal function without the help of any object.
 Unlike member functions, it cannot access the member names directly and has to
use an object name and dot membership operator with each member name.
 It can be declared either in the public or private part of a class without affecting its
meaning.
 Usually, it has the objects as arguments.

19
Friend Class
 A class can also be declared to be the friend of some other class. When we create a
friend class then all the member functions of the friend class also become the friend
of the other class. This requires the condition that the friend becoming class must
be first declared or defined (forward declaration).

20
21
 CONSTRUCTORS AND DESTRUCTORS

22
Constructors
 A constructor is a special member function whose task is to initialize the objects of
its class.
 It is special because its name is the same name as the class name.
 The constructor is invoked whenever an object of its associated class is created.
 It is called constructor because it constructs the values of data members of the
class.
 If you do not explicitly declare a constructor for a class, the C++ compiler
automatically generates a default constructor that has no arguments.

23
 A constructor is declared and defined as follows:
// class with a constructor
class integer
{
int m,n;
public:
integer(void); // constructor declared
…….
};
integer : : integer(void) //constructor defined
{
m=0; n=0;
}
int main()
{
integer obj;
………..
} 24
Characteristics of constructor
 They should be declared in the public section.
 They are invoked automatically when the objects are created.
 They do not have return values, not even void and therefore, and they cannot return
values.
 They cannot be inherited, though a derived class can call the base class constructor.
 Like other C++ functions, they can have default arguments.
 Constructors cannot be virtual.
 We cannot refer to their addresses.
 They make „implicit calls‟ to the operators new and delete when memory allocation
is required.

25
Types of Constructors
Constructors are of 3 types:
1. Default Constructor: A constructor that accepts no parameters is called the
default constructor.
2. Parameterized Constructor: The constructors that take parameters are called
parameterized constructors.
3. Copy Constructor: A copy constructor is used to declare and initialize an object
from another object.

26
Default Constructor

27
Parameterized Constructors
 The constructors that take parameters are called parameterized constructors.
 When a constructor has been parameterized, the object declaration statement such
as item t; may not work. We must pass the initial values as arguments to the
constructor function when an object is declared. This can be done in 2 ways:
item t=item(10,20); //explicit call
item t(10,20); //implicit call

28
Parameterized Constructors

29
Copy Constructor
 A copy constructor is used to declare and initialize an object from another object
Example: item t2(t1); or item t2=t1;.
1. The process of initializing through a copy constructor is known as copy
initialization.
2. t2=t1 will not invoke copy constructor. t1 and t2 are objects, assigns the values of
t1 to t2.
3. A copy constructor takes a reference to an object of the same class as itself as an
argument.

30
Copy Constructor

31
Multiple Constructors in a Class

32
Destructors
 A destructor, is used to destroy the objects that have been created by a
constructor.
 Like a constructor, the destructor is a member function whose name is the same
as the class name but is preceded by a tilde.
~item() { }
 A destructor never takes any argument nor does it return any value.
 It will be invoked implicitly by the compiler upon exit from the program to
clean up storage that is no longer accessible.
 It is a good practice to declare destructors in a program since it releases
memory space for future use.

33
Destructors

34
Operator overloading
 C++ has the ability to provide the operators with as special meaning for a data
type. The mechanism of giving such special meanings to an operator is known
as operator overloading. We can overload all the operators except the following:
 The operators that cannot be overloaded are:
• class member access operators (. , .*)
• scope resolution operator(: :)
• sizeof operator(sizeof)
• conditional operator(?:)

35
Operator overloading
 General form of operator function is:

return type classname :: operator op(argument list)


{
function body
}
 where return type is the type of value returned by the specified operation and op is
the operator being overloaded. The op is preceded by the keyword operator.
Operator op is the function name.
 Operator functions must be either member functions (non-static) or friend
functions.

36
Operator overloading
 The process of overloading involves following steps:
1.Create a class that defines the data type that is to be used in the overloading
operation.
2. Declare the operator function operator op() in the public part of the class. It may
be a member function or a friend function.
3. Here op is the operator to be overloaded.
4. Define the operator function to implement the required operations.

37
The operator functions are declared in the class using prototypes as follows:
 vector operator+(vector); // vector addition using member function
 vector operator-( ); // unary minus using member function
 friend vector operator+(vector, vector); // vector addition using friend function
 friend vector operator-(vector &a); // unary minus using friend function

38
Concept of Operator Overloading
 One of the unique features of C++ is Operator Overloading. Applying overloading
to operators means, same operator in responding different manner. For example
operator + can be used as concatenate operator as well as additional operator.
That is 2+3 means 5 (addition), where as
"2"+"3" means 23 (concatenation).
 Performing many actions with a single operator is operator overloading.

Benefits:
 Operator overloading concept can be applied in following two major areas
1. Extension of usage of operators
2. Data conversions

39
Rules to be followed for operator overloading
1. Only existing operators can be overloaded.
2. Overloaded operators must have at least one operand that is of user defined operators
3. We cannot change basic meaning of an operator.
4. Overloaded operator must follow minimum characteristics that of original operator
5. When using binary operator overloading through member function, the left hand operand must be
an object of relevant class.

The number of arguments in the overloaded operator‟ s arguments list depends


1. Operator function must be either member function or friend function.
2. If operator function is a friend function then it will have one argument for unary operator & two
arguments for binary operator
3. If operator function is a member function then it will have Zero argument for unary operator &
one arguments for binary operator

40
Unary Operator Overloading

• An unary operator means, an operator which works on single operand. For


example, ++ is an unary operator, it takess single operand (c++). So, when
overloading an unary operator, it takes no argument (because object itself is
considered as argument).

41
Program to overload unary operator

42
Program to overload unary minus operator using a friend function

43
Binary Operator Overloading

• An binary operator means, an operator which works on two operands. For


example, + is an binary operator, operand (c+d). So, when overloading an binary
operator, it takes one argument (one is object itself and other one is passed
argument).

44
Program for binary operator overloading

45
Program to overload arithmetic operators on complex numbers using
member function

46
Program to over load arithmetic operators on complex numbers using
friend function

47
Thank
You
48

You might also like