Oop Lab2 Final
Oop Lab2 Final
Time: 10:00 am –
1:00 pm
2:00pm – 5:00 pm
Amin
Objectives:
To learn difference between structures and classes
Implementing classes and understand data encapsulation in C++
Equipment/Software required:
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.
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.
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
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
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>
class Car {
private:
string make;
string model;
int year;
int mileage;
public:
void setMake(const string& m) {
make = m;
}
void setYear(int y) {
year = y;
}
void setMileage(int m) {
mileage = m;
}
int main() {
Car myCar;
myCar.setMake("Toyota");
myCar.setModel("Camry");
myCar.setYear(2020);
myCar.setMileage(15000);
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>
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;
}
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);
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.