0% found this document useful (0 votes)
8 views

Oop 02

Uploaded by

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

Oop 02

Uploaded by

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

Namal University, Mianwali

Department of Electrical Engineering

Course Title: CSC-101 Object Oriented Programming

LAB MANUAL

Lab 2

Introduction to Object Oriented Programming Concepts


- Classes and Objects, I

Student Name Roll Number


Suleman Ghani NIM-BSEE-2021-42

1
1. Objectives
The objective of this lab is to introduce students to the fundamental concepts of class and
objects in object-oriented programming through programming exercises. Students will
be provided with examples for creating classes and associated objects, followed by
performing Lab Tasks and Home Tasks based on class and objects.

2. Outcomes
• The student will be able to declare classes and objects.
• The student will be able to declare member functions and data item variables
ofa class.

3. Introduction
3.1 Class & Object:
The fundamental idea behind object-oriented languages is to combine into a single unit
both data and the functions that operate on that data. Such a unit is called an object. A
class serves as a plan, or blueprint. It specifies what data and what functions will be
included in objects of that class. An object is often called an “instance” of a class.
A class consists of Data and functions. The syntax of a class is as follows,
Syntax:
class class_name {
access_specifier_1:
members; // Data
access_specifier_2:
members; // functions
}
Member functions and Variables
Member variables represent the characteristics of the object and member functions
represent the behavior of the object. For example length & width are the member
variables of class Rectangle and set_values (int,int), area() are the member functions.

4. Examples

4.1 Create a C++ program that defines a Distance class to represent distances in feet and
inches. The class should include methods to set a distance with given values, input a distance
from the user, and display the distance in the format feet'-inches". In the main function,
initialize one distance with preset values and get another from user input, then display both.

2
Solution:

#include <iostream>
using namespace std;
////////////////////////////////////////////////////////////////
class Distance // Distance class
{
private:
int feet; // feet part of distance
float inches; // inches part of distance
public:
// Member function to set distance using arguments
void setdist(int ft, float in)
{
feet = ft;
inches = in;
}
// Member function to get distance from user input
void getdist()
{
cout << "\nEnter feet: ";
cin >> feet;
cout << "Enter inches: ";
cin >> inches;
}
// Member function to display distance
void showdist()
{
cout << feet << "\'-" << inches << "\"";
}
};
////////////////////////////////////////////////////////////////
int main()
{
Distance dist1, dist2; // define two lengths
dist1.setdist(11, 6.25); // set dist1 with preset values
dist2.getdist(); // get dist2 from user input
// display lengths
cout << "\ndist1 = ";
dist1.showdist();
cout << "\ndist2 = ";
dist2.showdist();
cout << endl;
return 0;
}

OUTPUT:

3
Enter feet: 10
Enter inches: 4.67
dist1 = 11'-6.25"
dist2 = 10'-4.67"

5. Lab Tasks

5.1 Create a class named time, the data members are hours, minutes and seconds. Write a
member function to read the data members supplied by the user, write a function to display the
data members in standard (24) hour and also in (12) hour format.

C++ Code:
#include <iostream>

using namespace std;

class Marks {
private:
int marks1;
int marks2;
int marks3;

public:
// Function to input marks
void set_marks() {
cout << "Enter marks of student 1: ";
cin >> marks1;
cout << "Enter marks of student 2: ";
cin >> marks2;
cout << "Enter marks of student 3: ";
cin >> marks3;
}

// Function to calculate and return the sum of marks


double sum() {
return marks1 + marks2 + marks3;
}

// Function to calculate and return the average of marks


double avg() {
return sum() / 3.0;
}
};

int main() {
Marks m;
m.set_marks();
cout << "Sum of marks: " << m.sum() << endl;
4
cout << "Average of marks: " << m.avg() << endl;
return 0;
}
Output:

Description:
Three private data members (`mark1`, `mark2`, and `mark3`) are defined in the class `marks` in this code
to represent a student's grades. Three member functions are included in this code to set the marks,
determine their sum, and determine the average. The `main()` function creates an object called `student`,
assigns marks, and displays the average and total scores.

5.2 Write a class marks with three data members to store three marks. Write three member
functions, set_marks() to input marks, sum() to calculate and return the sum and avg() to
calculate and return average marks.
C++ Code:
#include <iostream>
using namespace std;

class Time {
private:
int hours;
int minutes;
int seconds;

public:
// Function to read time from the user
void readTime() {
std::cout << "Enter hours: ";
std::cin >> hours;
std::cout << "Enter minutes: ";
std::cin >> minutes;
std::cout << "Enter seconds: ";
std::cin >> seconds;
}

5
// Function to display time in 24-hour format
void display24HourFormat() {
std::cout << "Time in 24-hour format: "
<< hours << ":"
<< minutes << ":"
<< seconds << std::endl;
}

// Function to display time in 12-hour format


void display12HourFormat() const {
int displayHours = hours % 12;
if (displayHours == 0) {
displayHours = 12; // Adjust for midnight and noon
}
std::string period;
if (hours >= 12) {
period = "PM";
} else {
period = "AM";
}

std::cout << "Time in 12-hour format: "


<< displayHours << ":"
<< minutes << ":"
<< seconds << " "
<< period << std::endl;
}
};

int main() {
Time t;
t.readTime();
t.display24HourFormat();
t.display12HourFormat();
return 0;
}
Ouput:

6
Descrition:
I define a `Time` class in this lab task that manages time in both 12-hour and 24-hour formats. The class
offers methods to set the time set_time and contains private data members for hours, minutes, and seconds.
input time from the readtime function, and use the display function display_24 and 12-hour format
display_12 to show the time in both 24-hour and 12-hour formats. The program asks the user to enter the
time in the `main()` function, then shows it in both formats, changing it to AM/PM for the 12-hour format.

6. Home Tasks

6.1 Write a program that declares a class Person. The class Person contains two data items
(name and age) and three-member functions (set_name, set_age, display_info). Use
memberfunctions to set information of two persons.

C++ Code:
#include <iostream>
#include <string>
using namespace std;
class Person
{
private:
string name;
int age;

public:
void set_name(string person_name)
{
name = person_name;
}
void set_age(int person_age)
{
age = person_age;
}
void display_info()
{
cout << "Person Name = " << name << endl;
cout << "Person Age = " << age << endl;
}
};

int main() {
Person p1, p2;
p1.set_name("Salman Sohi");
p1.set_age(19);
p2.set_name("Malik Waheed");
p2.set_age(32);
cout << " First Person : "<<endl;
p1.display_info();
cout << " Second Person : "<<endl;
7
p2.display_info();

return 0;
}
Output:

Description:
For this task, I define a class called `Person` that contains member functions to set and display the person's
name and age, as well as data members to store these details. To show how to set and display the data of
two distinct individuals that was provided in the main section of the code, two objects of the `Person` class
are created.

Conclusion:
In this lab I had concluded by appling the ideas of object-oriented programming in this lab. In order to
illustrate the use of member functions, excess specifiers, and assigning the value to the data item, we
developed classes to model real-world objects like Person, marks, and Time. Additionally, I investigated
how well the 24-hour and 12-hour time formats worked and effectively computed student grades. I have
learnt the fundamentals of class structure and function implementation in this lab.

8
Lab Evaluation Rubrics
Performance Unsatisfactory Marginal Satisfactoy Exemplary Allocated
Domain Rubric Indicator 0-5 5-10 11-15 16-20 Marks
Does not know how to Is able to use some of the Is able to use all the Able to use all the functions
use Software tool functions of Software tool functions of Software tool of Software tool required
Lab Task
R1 relevant to the lab. required in an experiment with required in an experiment in an experiment with little 20
Viva
errors and repeated guidance. with errors and repeated to no guidanceand without
guidance. any error.
Psychomotor

Does not try to solve Does not suggests or refine Refines solutions Actively looks for and
problems. Many solutions but is willing to try suggested by others. suggests solution to
mistakes in code and out solutions suggested by Complete and error-free problems. Complete and error
difficult to others. Few mistakes in code, code is done. No free code is done, easy to
R2 Implementation 20
comprehend for the but done along with comments, comments in the code, but comprehend for the
with Results instructor. There is and easy to comprehend for the easy to comprehend for the instructor. Results are
(P) not result of the instructor. Few mistake in instructor. Results are correctly produced. Student
problem. result. correctly produced. incorporated comments in the
code.
Code of the problem Code of the problem is not Code of the problem is not Code of the problem is not
is not given. Outputs given. Output is not complete. given. Output is completely given. Output is completely
Lab Report (A) are not provided. Explanation of the solution is given. Explanation of the given. Explanation of the
R3 20
Explanation of the not satisfactory. solution is not satisfactory. solution is satisfactory.
Affective

solution is not stated.

You might also like