You are on page 1of 4

OOPS-LAB-6

Q-1

#include <iostream>
using namespace std;
class TIME{

public:
int hours;
int minutes;
TIME( int h =0 , int m =0){
hours=h;
minutes=m;
}
};
void addTimes( TIME& t1, TIME& t2 , TIME& t3){
t3.hours = t1.hours + t2.hours ;
t3.minutes = t1.minutes + t2.minutes ;
if(t3.minutes>=60){
t3.hours += t3.minutes/60;
t3.minutes %= 60;
}

}
int main(){
TIME t1(3 , 45);
TIME t2(1 , 30);
TIME t3;
addTimes( t1 , t2 , t3);
cout<<"t1:"<<t1.hours<<"hours, "<<t1.minutes<<"minutes"<<endl;
cout<<"t2:"<<t2.hours<<"hours, "<<t2.minutes<<"minutes"<<endl;
cout<<"t3:"<<t3.hours<<"hours, "<<t3.minutes<<"minutes"<<endl;
return 0;

Q-2

#include <iostream>
// #include <string.h>
using namespace std;
class Student{
public:
string name;
int age;
Student( const string& n , int a){
name = n ;
age = a ;
}
};
double CalculateAverageAge( const Student& S1 , const Student& S2){
return (S1.age + S2.age)/2.0 ;
}
int main(){
Student S1("harsh", 19);
Student S2("rajat", 20);
double AverageAge = CalculateAverageAge(S1 , S2);
cout<<"Student 1 name :"<<S1.name<< " AGE:"<<S1.age<<"years"<<endl;
cout<<"Student 2 name :"<<S2.name<< " AGE:"<<S2.age<<"years"<<endl;
cout<<"Average AGE: "<<AverageAge<<"years"<<endl;
return 0;
}

Q-3

#include <iostream>

using namespace std;

class Rectangle {
private:
double length;
double width;

public:

Rectangle(double l, double w) : length(l), width(w) {}

double calculateArea() {
return length * width;
}
Rectangle getAreaRectangle() {
double area = calculateArea();
return Rectangle(length, width, area);
}

Rectangle(double l, double w, double a) : length(l), width(w) {


cout << "Creating a new Rectangle object with area " << a << endl;
}
};

int main() {
double length, width;

cout << "Enter the length of the rectangle: ";


cin >> length;

cout << "Enter the width of the rectangle: ";


cin >> width;

Rectangle rectangle(length, width);

Rectangle areaRectangle = rectangle.getAreaRectangle();

return 0;
}

Q-4

#include <iostream>
#include <string>

using namespace std;

class Student {
private:
string name;
int age;
string course;
public:
Student(string n, int a, string c) : name(n), age(a), course(c) {}

void displayDetails() {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "Course: " << course << endl;
}
};

int main() {
const int numStudents = 3;

Student students[numStudents] = {
Student("John", 20, "Computer Science"),
Student("Alice", 21, "Mathematics"),
Student("Bob", 19, "Physics")
};

cout << "Student Details:" << endl;


for (int i = 0; i < numStudents; i++) {
cout << "Student " << (i + 1) << ":" << endl;
students[i].displayDetails();
cout << endl;
}

return 0;
}

You might also like