Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Lab 03

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Experiment 03: Develop and execute a C++ program for Classes and Objects.

Lab. Objective: To program and simulate classes and objects in C++.

Tool: Dev C++ simulator.


Theory:

Everything in C++ is associated with classes and objects along with its attributes and
methods. For example car is an object. The car has attributes such as weight and color,
and methods such as drive and brake.

Attributes and methods are basically variables and functions that belong to the class.
These are referred to as class members.

A class is a user-defined data type that we can use in our program and it works as an
object constructor or a blueprint for creating objects.

An object is created from a class. We have already created the class named MyClass,
so now we can use this to create objects.

To create an object of MyClass, specify the class name, followed by the object name.

Task 1:
C++ program for Car class and its objects.
C++ Code:
#include<iostream>
using namespace std;
// Create a Car class with some attributes
class Car {
public:string brand;
string model;
int year;
};
int main() {
// Create an object of Car

Page 1 of 5
Car carObj1;
carObj1.brand = "BMW";
carObj1.model = "X5";
carObj1.year = 1999;
// Create another object of Car
Car carObj2;
carObj2.brand = "Ford";
carObj2.model = "Mustang";
carObj2.year = 1969;
// Print attribute values
cout << carObj1.brand << " " << carObj1.model << " " << carObj1.year << "\n";
cout << carObj2.brand << " " << carObj2.model << " " << carObj2.year << "\n";
return 0;
}
Results:

Results Analysis
In the above task we perform C++ program for Car class and its objects(mean that car
string brand, string model, and its years).
Task 2:
C++ program for a Room class and its objects.
C++ Code:
// Program to illustrate the working of room objects and room class in C++ Programming
#include <iostream>

Page 2 of 5
using namespace std;
// create a class
class Room {
public:
double length;
double breadth;
double height;
double calculateArea() {
return length * breadth;
}
double calculateVolume() {
return length * breadth * height;
}
};
int main() {
// create object of Room class
Room room1;
// assign values to data members
room1.length = 42.5;
room1.breadth = 30.8;
room1.height = 19.2;
// calculate and display the area and volume of the room
cout << "Area of Room = " << room1.calculateArea() << endl;
cout << "Volume of Room = " << room1.calculateVolume() << endl;
return 0;
}
Output Results:

Page 3 of 5
Results Analysis
In the above task we perform C++ program for a Room class and its objects (mean that
its length, breath and also its height)
Lab Assignment:
Write a C++ program that determine and display a room height, width and length using
classes and objects.
// Program to illustrate the working of room objects and room class in C++ Programming
#include <iostream>
using namespace std;
// create a class
class Room {
public:
double length;
double breadth;
double height;
double calculateArea() {
return length * breadth;
}
double calculateVolume() {
return length * breadth * height;
}
};
int main() {

Page 4 of 5
// create object of Room class
Room room1;
// assign values to data members
room1.length = 15.7;
room1.breadth = 10.3;
room1.height = 12.6;
// calculate and display the area and volume of the room
cout << "Area of Room = " << room1.calculateArea() << endl;
cout << "Volume of Room = " << room1.calculateVolume() << endl;
return 0;
}
Output Results:

Results Analysis
In the above task we perform C++ program for a Room class and its objects (mean that
its length, breath and also its height)
Conclusion:
Classes and objects is programmed and simulated in C++.

Page 5 of 5

You might also like