0% found this document useful (0 votes)
93 views7 pages

Lab 02 - Oop

The document discusses classes and constructors in C++. It defines what a class and object are, and provides examples of defining a class with data members and member functions. It explains access specifiers like private and public. It discusses encapsulation and how constructors are used to initialize objects. It provides examples of default and parameterized constructors. Finally, it lists two lab tasks - the first involves defining a Laptop class with attributes and constructor, the second involves calculating the average of user-input float values stored in an array class.

Uploaded by

junaid khan
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)
93 views7 pages

Lab 02 - Oop

The document discusses classes and constructors in C++. It defines what a class and object are, and provides examples of defining a class with data members and member functions. It explains access specifiers like private and public. It discusses encapsulation and how constructors are used to initialize objects. It provides examples of default and parameterized constructors. Finally, it lists two lab tasks - the first involves defining a Laptop class with attributes and constructor, the second involves calculating the average of user-input float values stored in an array class.

Uploaded by

junaid khan
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/ 7

Riphah International University

I-14 Main Campus


Faculty of Computing

Class: Spring-2021 Subject: Object Oriented Programming


Course Code: Class/Lab Instructor: M. Arif

-------------------- LAB 02 --------------------


Classes and Types of Constructor
Learning Objective:
1. Classes and Object Creation
2. Difference between structure and classes.
3. Access specifies. How to use it?
4. Constructor and its type.
5. Practical walk through.
6. Exercises

Classes and Objects

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.

Access specifies (Private, Public and protected)

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>

using namespace std;

class Test

private:

int data1;

float data2;

public:

void insertIntegerData(int d)

data1 = d;

cout << "Number: " << data1;

float insertFloatData()

cout << "\nEnter data: ";

cin >> data2;

Page 3 of 7
return data2;

};

int main()

Test o1, o2;

float secondDataOfObject2;

o1.insertIntegerData(12);

secondDataOfObject2 = o2.insertFloatData();

cout << "You entered " << secondDataOfObject2;

return 0;

Constructor

A class constructor is a special member function of a class that is executed whenever we


create new objects of that class.

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>

using namespace std;

class Line {

private:

double length;

public:

void setLength( double len );

Page 4 of 7
double getLength( void );

Line(); // This is the constructor

};

// Member functions definitions including constructor

Line::Line(void) {

cout << "Object is being created" << endl;

void Line::setLength( double len ) {

length = len;

double Line::getLength( void ) {

return length;

// Main function for the program

int main() {

Line line;

// set line length

line.setLength(6.0);

cout << "Length of line : " << line.getLength() <<endl;

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

// example: class constructor

#include <iostream>

using namespace std;

class Rectangle {

int width, height;

public:

Rectangle (int,int);

int area () {

return (width*height);

};

Rectangle::Rectangle (int a, int b) {

width = a;

height = b;

int main () {

Rectangle rect (3,4);

Rectangle rectb (5,6);

cout << "rect area: " << rect.area() << endl;

cout << "rectb area: " << rectb.area() << endl;

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:

1. Rectangle rect (3,4);

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

You might also like