Lab 02 - Oop
Lab 02 - Oop
A class is like a blueprint of data member and functions and object is an instance of class. For
example, let’s say we have a class Car which has data members (variables) such as speed,
weight, price and functions such as gearChange(), slowDown(), brake() etc. Now let’s say I
create an object of this class named FordFigo which uses these data members and functions
and give them its own values. Similarly we can create as many objects as we want using the
blueprint(class).
Example
class Car
//Data members
char name[20];
int speed;
int weight;
public:
Page 1 of 7
//Functions
void brake(){
void slowDown(){
};
int main()
//ford is an object
Car ford;
Encapsulation
Encapsulation is a process of combining data and function into a single unit like capsule. This
is to avoid the access of private data members from outside the class. To achieve
encapsulation, we make all data members of class private and create public functions, using
them we can get the values from these data members or set the value to these data members.
The body of the class contains two unfamiliar keywords: private and public. A key feature of
OOP is data hiding; means that data is concealed within a class, so that it cannot be accessed
mistakenly by functions outside the class. The primary mechanism of hiding data is to put it
in a class and make it private. Private data or functions can only be accessed from within the
class. Public data or functions, on the other hand, are accessible from outside the class.
Usually the data within a class is private and the functions are public. This is a result of how
classes are used. The data is hidden so it will be safe from accidental manipulation, while
functions that operate on the data are public so they can be accessed from outside the class.
However, there is no rule that data must be private and functions public; in some
circumstances you may find you’ll need to use private functions and public data.
Page 2 of 7
How to define a class in C++
A class is defined in C++ using keyword class followed by the name of class.The body of
class is defined inside the curly brackets and terminated by a semicolon at the end.
class className
// some data
// some functions
};
Example
#include <iostream>
class Test
private:
int data1;
float data2;
public:
void insertIntegerData(int d)
data1 = d;
float insertFloatData()
Page 3 of 7
return data2;
};
int main()
float secondDataOfObject2;
o1.insertIntegerData(12);
secondDataOfObject2 = o2.insertFloatData();
return 0;
Constructor
A constructor will have exact same name as the class and it does not have any return type at
all, not even void. Constructors can be very useful for setting initial values for certain
member variables.
Example
#include <iostream>
class Line {
private:
double length;
public:
Page 4 of 7
double getLength( void );
};
Line::Line(void) {
length = len;
return length;
int main() {
Line line;
line.setLength(6.0);
return 0;
Parameterize Constructor
A default constructor does not have any parameter, but if you need, a constructor can have
parameters. This helps you to assign initial value to an object at the time of its creation as
shown in the following example.
Page 5 of 7
Example
#include <iostream>
class Rectangle {
public:
Rectangle (int,int);
int area () {
return (width*height);
};
width = a;
height = b;
int main () {
return 0;
class Rectangle has no member function set_values, and has instead a constructor that
performs a similar action: it initializes the values of width and height with the arguments
passed to it. .
Page 6 of 7
Notice how these arguments are passed to the constructor at the moment at which the objects
of this class are created:
Lab Tasks:
1. Write a C++ program that creates a class called laptop. The data members of the
class are
brand (string)
model(string)
serial (int)
color (string)
price (float)
processor speed (float)
RAM (int)
screen size(float).
The constructor should accept the laptop brand, model, serial, color, price,
processor speed, ram and screen size.
Make a member function name display to display the value of all attributes.
Make 3 objects of laptop and display its values.
2. Write a program that uses an array to find the Average of a set float values entered by
the user.
You will have the main function control the operation of the program, but all values
will be stored in the class.
You will need to use get and set methods in your class.
Create an array as a member of the class.
Create 1 method to handle user input.
Create 1 method to sum all numbers in array
Create 1 method to output the average of all numbers input by the user.
Create 1 method to output all the numbers input by the user.'
Page 7 of 7