Oop Lec 3
Oop Lec 3
Lecture 3
• Model
• Object
Stores Change internal state
• State Fields
• Behavior
Implementation
• Identity
Interfaces
Exposes their
functionality through
3
Abstraction
• Principle of abstraction:
4
Abstraction - Example
5
Abstraction - Example
6
Abstraction – Advantages
7
Class
Mammals
• We say that they belong to the same class
Humans 8
Class
• What is Class?
o Class is a blueprint from which individual objects are created
width radius
Class
Data Memebers
height color
10
Few More Examples – Class
11
Graphical Representation of Classes
(Class Name)
(Class Name)
(states)
Suppressed
(behavior) Form
Normal Form
Example - Graphical Representation of Classes
Circle
• center Circle
• radius Suppressed
Suppressed
draw() Form
Form
computeArea()
Normal
NormalForm
Form
13
Example - Graphical Representation of Classes
Person
• Name
• Age Person
• Gender
Suppressed
eat() Form
walk()
Normal Form
14
Member Functions
• Complete example:
Accessor
Functions
Member Functions (contd.)
• Outside Class:
Scope Operator
Class: Scope Operator
• Another Example:
#include <iostream>
using namespace std;
class Student
{
int rollNo;
public:
void setRollNo(int aRollNo);
}; Scope Operator
Disadvantages:
1. Too many inline functions will increase the size of the binary executable because
of the duplication of same code.
2. Inline function may increase compile time overhead. If someone changes the
code inside the inline function then all the calling location has to be recompiled.
3. Inline functions may not be useful for many embedded systems. Because in
embedded systems code size is more important than speed.
Inline Functions and Classes
• If we define the function inside the class body then the function is by
default an inline function
• In case function is defined outside the class body then we must use
the keyword ‘inline’ to make a function inline
Inline Functions and Classes: Example
#include <iostream>
using namespace std;
class Student
{
int rollNo;
public:
inline void setRollNo(int aRollNo);
};
• Constructor is used to ensure that object is in well defined state at the time
of creation
• Declared just like regular member function, but with a name that matches
the class name and without any return type; not even void
Class: Constructor
Output:
Types of Constructor
• Default constructor
• Parameterized constructor
• Copy constructor
Default Constructor
• Complete Example
default constructor
Constructors Overloading
Note:
Whenever we define one or more non-default constructors( with
parameters ) for a class, a default constructor( without parameters
) should also be explicitly defined as the compiler will not provide a
default constructor in this case
Constructors Overloading
• Is equivalent to
Rectangle::Rectangle()
Rectangle::Rectangle(int a)
Rectangle::Rectangle(int a, int b)
Class: Destructor
• Example
Dynamic memory
•There are two ways that memory gets allocated for data storage
•Compile Time (or static) Allocation
• Memory for named variables is allocated by the compiler
• Exact size and type of storage must be known at compile time
• For standard array declarations, this is why the size has to be constant
•Dynamic Memory Allocation
• Memory allocated "on the fly" during run time
• Exact amount of space or number of items does not have to be known
by the compiler in advance.
• For dynamic memory allocation, pointers are crucial
Dynamic memory
• Examples
int * p = new int; // dynamic integer, pointed to by p
*p = 10; // assigns 10 to the dynamic integer
cout << *p; // prints 10
Problem:
Cant create a shape object with different
number of sides at runtime.
Dynamic memory
Output:
Dynamic memory
Output:
Sequence of Calls
Sequence::Sequence(int a)
{
check = a;
cout << "I am in constructor " << check << endl;
}
Sequence::~Sequence()
{
cout << "I am in destructor " << check << endl;
}