Lab 2
Lab 2
Class: A blue print for creating objects. (Consider it as map of your house, you need only one map to
construct as many houses as you want)
Object: Instance of a class. (Consider it as your house, each house constructed is one instance of a
class)
Features of OOP
There are four basic features, more can be derived from these.
1. Encapsulation
2. Abstraction
3. Inheritance
4. Polymorphism
All of the above have been discussed in class deeply. Encapsulation is the binding of data and functions
acting upon that data. Abstraction is hiding of implementation/functionality except relevant data.
Inheritance is using functionalities of other classes (like a child inherits properties from
parents)(Reusing). Polymorphism has two types
Access Specifiers
Members(data members and functions) can be specified having any of the following properties. By
default, all members are private.
Welcome Back
Note: Function can be defined outside class but it needs to be declared inside class. We will see it in
coming lectures.
Constructor
We have seen the functions/methods can be called through function’s objects. Constructor is special
type of function that does not need to be called. It is called as soon as object is created automatically.
Constructor has no return type but it can accept parameters and it’s name is same as class name.
Constructor is usually used to initialize values.
Syntax:
It can be seen that we haven’t called the TestConstructor() function but the values are initialized.
Default Constructor:
The constructor that takes no arguments is called default constructor. If we don’t write any
constructor, when the program is complied, c++ automatically write a default constructor that does
nothing.
Parameterized Constructor:
The constructor that takes arguments is called parameterized constructor.
Note: If you use parameterized constructor, default constructor must be defined otherwise it will
cause error, if you instantiate class without parameters (object without parameters).
Destructor:
Destructor is a member function which destructs or deletes an object. Destructors have same name
as the class preceded by a tilde (~). Destructors don’t take any argument and don’t return anything
and they don’t need to be called. A class can only have one destructor. If we do not write our own
destructor in class, compiler creates a default destructor for us. The default destructor works fine
unless we have dynamically allocated memory or pointer in class. (Will discuss it later in the course)
Exercise:
1. Create a class Circle with one data member radius. Write two member functions, area() and
circumference().
2. Create a constructor in above program to initialize radius to zero.
3. Add overloaded constructor to circle class. The constructor should accept integer as
argument and use it as radius to calculate area and circumference.
4. Write a statement that defines an array of five objects of circle class. Let default constructor
execute for each elements of an array.
5. Pass 12, 7, 9, 14 and 8 as arguments to constructor.
6. Write a for loop that displays radius and area of circles represented by array.
7. Create a destructor for class Circle