0% found this document useful (0 votes)
22 views11 pages

Oop Lab2 Final

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)
22 views11 pages

Oop Lab2 Final

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/ 11

School Of Electrical Engineering

And Computer Science

CS212: Object Oriented

Programming Class: BEE-15ABC

Lab 2: Classes and Objects

Date: 24 September 2024

Time: 10:00 am –

1:00 pm

2:00pm – 5:00 pm

Instructor: Dr. Sadiq

Amin

Lab Engineer : Engr. Syed Zain Ul Hassan

STUDENTS: M HARIS NAVEED 474289


ANAS ALI 365750
M HUSSAIN 457358
EXPERIMENT NO 2

Introduction to Classes in C++

Objectives:
To learn difference between structures and classes
Implementing classes and understand data encapsulation in C++

Equipment/Software required:

Visual Studio/ Dev C++

Theory
The main purpose of C++ programming is to add object orientation to the C programming
language and classes are the central feature of C++ that supports object-oriented
programming and are often called user-defined types. A class is a user defined type that has
data and functions

Defining a Class

A class definition begins with the keyword class. The body of the class is contained
within a set of braces, { }; A class consists of its members. These members can be
either attributes or functions.

Figure 1: Class Body


Access specifiers

Visibility of attributes and functions in class are set within the body using access specifiers.
The keywords private: and public: specify the access level of the members of the class.
• Private: class members are only visible within a class body i.e. within public, private
member functions. These are not accessible outside the class body.
• Public: class members are visible both within and outside the class body.

Default access specifier is private. It is important to note that private members cannot be
accessed directly using direct member access operator (.). We will learn how private
members can be accessed.

Declaring objects of a class

A class provides the blueprints for objects, so basically an object is created from a class.
We declare objects of a class with exactly the same sort of declaration that we declare
variables of basic types. This is also called instantiation. You can have many variables of
the same type (class) but different names in same scope. Once an object of a certain class is
instantiated, a new memory location (separate for all objects) is created for it to store its
data members and code.
You can instantiate many objects from a class type. For example

• Foo c; // creating an object or declaring a variable of user defined data type


• Foo *a;// creating pointer to an object
• Foo b[100]; // creating array of objects

The public data members of objects of a class can be accessed using the direct member
access operator (.).Let us try the following example to make the things clear:
Calling member functions
int main()
{
Foo s1, s2; //instances
s1.setData(1066); s2.setData(1776); //calling public member functions using (.) operator
s1.getData(); s2.getData(); return 0;
}

Private data members of class are not visible outside the class but they are visible within the
class. Thus to access private data members of class, public member functions are used due
to the reason of their visibility outside the class. Making the attributes private and hiding
them from the entire outer world helps in attaining one of the main objectives of Object
Oriented programming i.e. Encapsulation. Member functions that are used to access private
data members to set some value are called setters and member functions used to retrieve
value of private data members are called getters.

LAB TASKS

I. Create a class Rectangle keeping in mind that rectangle have length and width. One
should be able to calculate the perimeter () and the area() of the rectangle, can set a
value to length and width and retrieve the value of Length & width. Where

0 < length < 21


0 < width < 21
Area of Rectangle=length*width;
Perimeter of Rectangle=(length+width)*2;
TEST PLANS
Commands Output
Rectangle r;
r.set(2);
r.getLength();
r.set(50);
r.getLength();
Rectangle f=r;
f.set(-20);
f.getLength();

CODE:
#include<iostream>
using namespace std;
class rec {
public:
int l,w,per,a;
void getlength() {
cout <<"ENTER LENGTH OF RECTANGLE"<< endl;
cin >> l;
cout <<"ENTER WIDTH OF RECTANGLE"<< endl;
cin >> w;}

void perimeter() {
per=(l+w)*2;
cout<<"PERIMETER IS"<< per<< endl;

}
void area() {
a=l*w;
cout<<"AREA IS"<< a;
}
};
int main () {
rec rec1;
rec1.getlength();
rec1.perimeter();
rec1.area();
return 0;
}

Create a class called Car that has the following data members:


make (string): the make of the car


model (string): the model of the car


year (int): the year the car was manufactured
mileage (int): the total mileage on the car


The Car class should have the following member functions:


setMake(string make): sets the make of the car


setModel(string model): sets the model of the car


setYear(int year): sets the year the car was manufactured


setMileage(int mileage): sets the total mileage on the car


getMake() const: returns the make of the car


getModel() const: returns the model of the car


getYear() const: returns the year the car was manufactured
getMileage() const: returns the total mileage on the car
In your main function, create an object of the Car class and set its data members using the
member functions. Then, output the data members to the console.

CODE:
#include <iostream>
#include <string>

using namespace std;

class Car {
private:
string make;
string model;
int year;
int mileage;

public:
void setMake(const string& m) {
make = m;
}

void setModel(const string& m) {


model = m;
}

void setYear(int y) {
year = y;
}

void setMileage(int m) {
mileage = m;
}

string getMake() const {


return make;
}

string getModel() const {


return model;
}

int getYear() const {


return year;
}

int getMileage() const {


return mileage;
}
};

int main() {
Car myCar;
myCar.setMake("Toyota");
myCar.setModel("Camry");
myCar.setYear(2020);
myCar.setMileage(15000);

cout << "Car Make: " << myCar.getMake() << endl;


cout << "Car Model: " << myCar.getModel() << endl;
cout << "Car Year: " << myCar.getYear() << endl;
cout << "Car Mileage: " << myCar.getMileage() << " miles" << endl;

return 0;}
OUTPUT:

Develop a program that implements a C++ class “Student” with following data members


Age


Roll Number


GPA


markSubject1


marksSubject2
marksSubject3
The class should define a method/function to calculate percentage from marks obtained in three
subjects.
Finally, you’ll create objects of Student class, initialize their data members through set and get
functions, calculate percentages, determine grades and print summary in the end.

CODE:

#include <iostream>
#include <string>

using namespace std;

class Student {
private:
int age;
int rollNumber;
float gpa;
float marksSubject1;
float marksSubject2;
float marksSubject3;

public:
void setAge(int a) {
age = a;
}

void setRollNumber(int r) {
rollNumber = r;
}

void setGPA(float g) {
gpa = g;
}

void setMarksSubject1(float marks) {


marksSubject1 = marks;
}

void setMarksSubject2(float marks) {


marksSubject2 = marks;
}

void setMarksSubject3(float marks) {


marksSubject3 = marks;
}

int getAge() const {


return age;
}

int getRollNumber() const {


return rollNumber;
}

float getGPA() const {


return gpa;
}

float getMarksSubject1() const {


return marksSubject1;
}

float getMarksSubject2() const {


return marksSubject2;
}

float getMarksSubject3() const {


return marksSubject3;
}

float calculatePercentage() const {


float totalMarks = marksSubject1 + marksSubject2 + marksSubject3;
return (totalMarks / 300) * 100;
}

char determineGrade() const {


float percentage = calculatePercentage();
if (percentage >= 90) return 'A';
else if (percentage >= 75) return 'B';
else if (percentage >= 50) return 'C';
else return 'F'; // Fail
}

void printSummary() const {


cout << "Roll Number: " << rollNumber << endl;
cout << "Age: " << age << endl;
cout << "GPA: " << gpa << endl;
cout << "Marks Subject 1: " << marksSubject1 << endl;
cout << "Marks Subject 2: " << marksSubject2 << endl;
cout << "Marks Subject 3: " << marksSubject3 << endl;
cout << "Percentage: " << calculatePercentage() << "%" << endl;
cout << "Grade: " << determineGrade() << endl;
cout << "--------------------------" << endl;
}
};

int main() {
Student student1;
student1.setAge(20);
student1.setRollNumber(101);
student1.setGPA(3.5);
student1.setMarksSubject1(85);
student1.setMarksSubject2(90);
student1.setMarksSubject3(78);

Student student2;
student2.setAge(22);
student2.setRollNumber(102);
student2.setGPA(3.7);
student2.setMarksSubject1(60);
student2.setMarksSubject2(55);
student2.setMarksSubject3(70);

cout << "Student 1 Summary:" << endl;


student1.printSummary();

cout << "Student 2 Summary:" << endl;


student2.printSummary();

return 0;
}

OUTPUT:
CONCLUSION:
In this lab on the introduction of classes, we explored the core concepts of object-oriented
programming. By defining classes and creating objects, we learned how to encapsulate data and
functions, making our code more organized and reusable. The lab emphasized how classes help in
structuring programs for better scalability and maintainability, enabling us to create modular code
that can be easily extended and adapted in future projects.

You might also like