Algorithm Programming: Object Oriented Programming
Algorithm Programming: Object Oriented Programming
OUTLINE
• The keyword public determines the access attributes of the members of the class
that follow it. A public member can be accessed from outside the class anywhere
within the scope of the class object. You can also specify the members of a class
as private or protected which we will discuss in a sub-section.
Classes and
MECHATRONICS STUDY PROGRAM Objects
Classes and Objects (3)
Define C++ Objects:
• A class provides the blueprints for objects, so basically an object is
created from 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:
• Both of the objects Box1 and Box2 will have their own copy of data
members.
Classes and
MECHATRONICS STUDY PROGRAM Objects
Classes and Objects (5)
Accessing the Data Members:
• The public data members of objects of a class can be accessed
using the direct member access operator (.). Let us try the
following example to make the things clear:
• Member functions can be defined within the class definition or separately using
scope resolution operator, ::. Defining a member function within the class
definition declares the function inline, even if you do not use the inline specifier.
So either you can define Volume() function as below:
Classes and
MECHATRONICS STUDY PROGRAM Objects
Class member functions (2)
• If you like you can define same function outside the class using scope
resolution operator, :: as follows:
• Here, only important point is that you would have to use class name just before ::
operator. A member function will be called using a dot operator (.) on a object
where it will manipulate data related to that object only as follows:
• Let us put above concepts to set and get the value of different class members in a
class: See and Run Program_2.cpp
Homework
MECHATRONICS STUDY PROGRAM
• If for a class C, you have multiple fields X, Y, Z, etc., to be initialized, then use
can use same syntax and separate the fields by comma as follows:
Homework
MECHATRONICS STUDY PROGRAM
• Defining a Structure:
– To define a structure, you must use the struct statement. The struct
statement defines a new data type, with more than one member, for
your program. The format of the struct statement is this:
MECHATRONICS STUDY PROGRAM
Constructor called.
Constructor called.
Box2 is equal to or larger than Box1
Classes and
MECHATRONICS STUDY PROGRAM Objects
Pointer to C++ classes (1)
A pointer to a C++ class is done exactly the same way as a pointer to a
structure and to access members of a pointer to a class you use the
member access operator -> operator, just as you do with pointers to
structures. Also as with all pointers, you must initialize the pointer before
using it.
Please see Program_15.cpp. When the Program_15.cpp code is
compiled and executed, it produces the following result:
Constructor called.
Constructor called.
Volume of Box1: 5.94
Volume of Box2: 102
Homework
MECHATRONICS STUDY PROGRAM
OUTLINE
C++ Inheritance
One of the most important concepts in object-oriented programming is that
of inheritance. Inheritance allows us to define a class in terms of another
class, which makes it easier to create and maintain an application. This
also provides an opportunity to reuse the code functionality and fast
implementation time.
Consider a base class Shape and its derived class Rectangle as follows in
Please see Program_17.cpp. When the Program_17.cpp code is
compiled and executed, it produces the following result:
Total area: 35
C++ Inheritance
MECHATRONICS STUDY PROGRAM
A derived class inherits all base class methods with the following
exceptions:
Constructors, destructors and copy constructors of the base class.
Overloaded operators of the base class.
The friend functions of the base class.
C++ Inheritance
MECHATRONICS STUDY PROGRAM
Where access is one of public, protected, or private and would be given for
every base class and they will be separated by comma as shown above. Let
us try see Program_18.cpp. When the Program_18.cpp code is compiled
and executed, it produces the following result:
Total area: 35
Total paint cost: $2450
MECHATRONICS STUDY PROGRAM
OUTLINE
C++ allows you to specify more than one definition for a function
name or an operator in the same scope, which is called function
overloading and operator overloading respectively.
Printing int: 5
Printing float: 500.263
Printing character: Hello C++
C++ Overloading
MECHATRONICS STUDY PROGRAM (Operator and Function)
The increment (++) and decrement (--) operators are two important
unary operators available in C++.
The unary operators take two arguments and following are the examples
of Binary operators. You use binary operators very frequently like
addition (+) operator, subtraction (-) operator and division (/) operator.
D2 is less than D1
C++ Overloading
MECHATRONICS STUDY PROGRAM (Operator and Function)
C++ is able to input and output the built-in data types using the stream
extraction operator >> and the stream insertion operator <<. The stream
insertion and stream extraction operators also can be overloaded to
perform input and output for user-defined types like an object.
Here, it is important to make operator overloading function a friend of the
class because it would be called without creating an object.
Following example explains how extraction operator >> and insertion
operator <<. Please see the example in Program_25.cpp. When the
above code is compiled and executed, it produces the following result:
Enter the value of object :
70
10
First Distance : F : 11 I : 10
Second Distance :F : 5 I : 11
Third Distance :F : 70 I : 10
C++ Overloading
MECHATRONICS STUDY PROGRAM (Operator and Function)
You can overload the assignment operator (=) just as you can other
operators and it can be used to create an object just like the copy
constructor.
The function call operator () can be overloaded for objects of class type.
When you overload ( ), you are not creating a new way to call a function.
Rather, you are creating an operator function that can be passed an
arbitrary number of parameters.
Value of A[2] : 2
Value of A[5] : 5
Index out of bounds
Value of A[12] : 0
C++ Overloading
MECHATRONICS STUDY PROGRAM (Operator and Function)
10
12
11
...
21
MECHATRONICS STUDY PROGRAM
OUTLINE
OUTLINE
OUTLINE
OUTLINE