0% found this document useful (0 votes)
209 views4 pages

Lab 1 OOP ANSWER

The document discusses four key concepts of object-oriented programming (OOP): 1. Abstraction refers to hiding unnecessary details from users and showing only essential features. 2. Encapsulation binds data and functions together in classes. 3. Inheritance allows code reuse by deriving a child class from a parent class. 4. Polymorphism enables functions to perform different actions depending on arguments passed. It also defines classes as blueprints for objects that group data and functions, and objects as instances of classes that have state and behavior. A sample program to print a name and roll number using OOP is presented.

Uploaded by

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

Lab 1 OOP ANSWER

The document discusses four key concepts of object-oriented programming (OOP): 1. Abstraction refers to hiding unnecessary details from users and showing only essential features. 2. Encapsulation binds data and functions together in classes. 3. Inheritance allows code reuse by deriving a child class from a parent class. 4. Polymorphism enables functions to perform different actions depending on arguments passed. It also defines classes as blueprints for objects that group data and functions, and objects as instances of classes that have state and behavior. A sample program to print a name and roll number using OOP is presented.

Uploaded by

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

Practical 1

Introduction to Object Oriented programming using C++


Exercise 1
Problem Statement 1:
What are the four basic concepts of OOP? Explain each of them by giving real life examples in your own
words.

ANS:

Abstraction
Abstraction refers to showing only the essential features of the application and hiding the details. In C+
+, classes can provide methods to the outside world to access & use the data variables, keeping the
variables hidden from direct access, or classes can even declare everything accessible to everyone, or
maybe just to the classes inheriting it. This can be done using access specifiers.

Example:
Abstraction is a mechanism with the help of which programmers hide the unnecessary
details from the users.

Encapsulation
It can also be said data binding. Encapsulation is all about binding the data variables and functions
together in class.

Example:
 the pencil box which you use daily, that pencil box contains other things also like eraser
sharpner etc. Same in the case of programming, the OOPS gives us the advantage of
gathering the data and functions in the same box which is called “Class”.
Inheritance
Inheritance is a way to reuse once written code again and again. The class which is inherited is called
the Base class & the class which inherits is called the Derived class. They are also called parent and child
class. So when, a derived class inherits a base class, the derived class can use all the functions which are
defined in base class, hence making code reusable.

Example:
The inheritance is beneficial for programming because with the help of this we can make our
code as short as possible and there will be not any kind of redundancy.
Polymorphism
It is a feature, which lets us create functions with same name but different arguments, which will
perform different actions. That means, functions with same name, but functioning in different ways. Or,
it also allows us to redefine a function to provide it with a completely new definition. You will learn how
to do this in details soon in coming lessons.

Example:
we use milk for drinking but milk can also be used to make curd, butter etc. So, the term
polymorphism means to use something in different forms. In programming, to maintain the
code and to maintain the simplicity of the code we use the concept of polymorphism. Very
popular example of this is, “function overloading”, in which we use the same name but the
operations are different.

Problem Statement 2:
What are classes and objects in OOP?

ANS:

Objects
Objects are the basic unit of OOP. They are instances of class, which have data members and uses
various member functions to perform tasks. Any entity that has state and behavior is known as an
object.

For example: chair, pen, table, keyboard, bike etc. It can be physical and logical.

Class
Class can also be defined as user defined data type but it also contains functions in it. So, class is
basically a blueprint for object. It declare & defines what data variables the object will have and what
operations can be performed on the class's object. It is a logical entity.
Problem Statement 3:
Write a simple program which will print “Your Name and Roll Number” in C++ using OOP concepts.
Display the output.

Problem Statement 4:
Define structure and function

ANS:

FUNCTION
A function is a group of statements that together perform a task. Every C++
program has at least one function, which is main () and all the most trivial
programs can define additional functions.

You can divide up your code into separate functions. How you divide up your code
among different functions is up to you, but logically the division usually is such
that each function performs a specific task.

STRUCTURE

A structure is defined as a collection of data items of either same or different data type
referred to by a common name. The various data items of a structure are known as
members (also known as elements or fields) of a structure.
.

You might also like