0% found this document useful (0 votes)
27 views3 pages

Lab 2

This document discusses object oriented programming concepts including classes, objects, encapsulation, abstraction, inheritance, polymorphism, access specifiers, constructors, and destructors. It provides examples of creating a Circle class with methods for calculating area and circumference. It describes creating default and parameterized constructors, defining an array of Circle objects using the default constructor, passing arguments to the parameterized constructor, and displaying radius and area values using a for loop. It also mentions creating a destructor for the Circle class.

Uploaded by

Furqan Nasir
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
27 views3 pages

Lab 2

This document discusses object oriented programming concepts including classes, objects, encapsulation, abstraction, inheritance, polymorphism, access specifiers, constructors, and destructors. It provides examples of creating a Circle class with methods for calculating area and circumference. It describes creating default and parameterized constructors, defining an array of Circle objects using the default constructor, passing arguments to the parameterized constructor, and displaying radius and area values using a for loop. It also mentions creating a destructor for the Circle class.

Uploaded by

Furqan Nasir
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 3

Object Oriented Programming Lab Lab 2

Object Oriented Programming


Introduction
The change from C to C++ was the introduction of Object Oriented Programming. The focus of OOP is
on modelling data(Focus is on data). Everything in OOP is grouped as self-sustainable “objects”.

Two basic things: Class and Object

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

a) Function overloading(Same function name with different signatures in same class)


b) Function overriding(Same function name with same signature in child classes with different
implementation)

Access Specifiers
Members(data members and functions) can be specified having any of the following properties. By
default, all members are private.

1. Public: Accessible everywhere in program


2. Private: Accessible only inside class(includes methods of class)
3. Protected: Accessible inside class and derived classes

Go to Lab 1 provided on LMS

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.

1|Page Furqan Nasir


Object Oriented Programming Lab Lab 2

Syntax:

It can be seen that we haven’t called the TestConstructor() function but the values are initialized.

Note: We cannot even use void in return type in constructor.

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

2|Page Furqan Nasir


Object Oriented Programming Lab Lab 2

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

3|Page Furqan Nasir

You might also like