Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Lab Manual

Experiment 2
Introduction to Object Oriented Programming
OBJECTIVE
 To understand and implement Classes
 To understand and implement Objects
THEORY
Object Oriented Programming(or OOP) allows you to represent real-life objects in programming.
These real-life objects have attributes and methods(functions or behaviours) allocated to them. This
concept of implementing object oriented programming is done using two basic concepts called
Classes and Objects.
A Class represents a template or a blueprint. It has all the basic properties and methods that can be
attributed to any object.A Class is basically a user-defined datatype.
An Object is basically an instance or example of the above defined Class.
For example , A fruit can be termed as a class and Apple , Banana and Orange can be called as an
object of the fruit class. The attributes are properties of the object. For this example, it can be the
size, color and sugar content of the fruit.
An example of creating a class is presented below:

Figure 1- Creating a Fruit Class with its attributes

The above attached picture is a demonstration of creating a class in C++ programming language. A
public keyword modifier is inserted just after the class is created to make sure that all of these
attributes and methods are accessible outside the class as well. If this keyword is not inserted , the
attributes of the class remain private and are not accessible outside the class.

SZABIST KARACHI
Lab Manual

Example of using creating an object from a class and using its properties is given below:

Figure 2- Object Creation of Class

This object is created within the main function of the program. This example shows how to assign
new values to a given object of a class and even access the given values already in it.

SZABIST KARACHI
Lab Manual

EXERCISE

1. Implement Class of Cars , Taking input from user about its attributes or properties
2. Implement Class of Courses , Taking input from user about its attributes or properties
3. Implement Class of Sensors , Taking input from user about its attributes or properties

SZABIST KARACHI

You might also like