Assignment No 2

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

1

NAME : RIMSHA JAVED


ID NO : F2023266400
SECTION : V15
ASSIGNMENT NO 2
LAB TASK 1
#include <iostream>
#include <string>
using namespace std;
class Person
{
public:
Person(string n, int a, std::string c)
{
name = n;
age = a;
city = c;
}
void inputDetails()
{
cout << "Input for person" <<endl;
cout << name << ", ";
cin >> age;
2

cout << ", " << city <<endl;


}
int getAge()
{
return age;
}
void display() {
cout << name << ", " << age << ", " << city <<endl;
}
private:
string name;
int age;
string city;
};
int main() {
Person p1("Ali", 23, "Lahore");
Person p2("Hassan", 20, "Jhang");
p1.inputDetails();
p2.inputDetails();
int age1 = p1.getAge();
int age2 = p2.getAge();
if (age1 > age2)
{
p1.display();
cout << " elder in age" <<endl;
3

} else if (age2 > age1)


{
p2.display();
cout << " elder in age" <<endl;
} else
{
p1.display();
cout << " same age" << endl;
p2.display();
cout << "same age" << endl;
}
return 0;
}

OUTPUT
4

LAB TASK 2
#include <iostream>
Using namespace std;
class Rectangle
{
Public:
Rectangle(double l, double w)
{
setLength(l);
setWidth(w);
}
double getLength()
{
return length;
}
void setLength(double l)
{
if (l > 0) {
length = l;
}
}
double getWidth()
{
return width;
}
5

void setWidth(double w)
{
if (w > 0)
{
width = w;
}
}
double getArea()
{
return length * width;
}
private:
double length;
double width;
};

int main()
{
Rectangle rect(5, 3);
cout << "Length: " << rect.getLength() <<endl;
cout << "Width: " << rect.getWidth() << endl;
cout << "Area: " << rect.getArea() <<endl;
rect.setLength(10);
rect.setWidth(6);
cout << "Length: " << rect.getLength() <<endl;
6

cout << "Width: " << rect.getWidth() << endl;


cout << "Area: " << rect.getArea() << endl;
return 0;
}

OUTPUT
7

LAB TASK 3
#include <iostream>
#include <string>
Using namespace std;
class Student
{
public:
Student(string n, string i, string a, float g)
{
setName(n);
setID(i);
setAddress(a);
setGPA(g);
}
string getName()
{
return name;
}
void setName(string n)
{
name = n;
}
string getID()
{
return ID;
8

}
void setID(string i) {
ID = i;
}
string getAddress()
{
return address;
}
void setAddress(string a)
{
address = a;
}
float getGPA()
{
return GPA;
}
void setGPA(float g) {
if (g >= 0.0 && g <= 4.0) {
GPA = g;
}
}
void printInfo()
{
cout << "Name: " << name <<endl;
cout << "ID: " << ID << endl;
9

cout << "Address: " << address << endl;


cout << "GPA: " << GPA <<endl;
}

private:
string name;
string ID;
string address;
float GPA;
};

int main()
{
Student student("John Doe", "123456", "123 Main St", 3.5);
student.printInfo();
student.setName("Jane Doe");
student.setID("987654");
student.setAddress("456 Elm St");
student.setGPA(3.8);

student.printInfo();

return 0;
}
10

OUTPUT
11

LAB TASK 4
#include <iostream>
Using namespace std;
class Car
{
private:
string make;
string model;
int year;
string color;

public:
string getMake()
{
return make;
}
void setMake(string makeIn)
{
make = makeIn;
}
string getModel()
{
return model;
}
void setModel(string modelIn)
12

{
model = modelIn;
}
int getYear()
{
return year;
}
void setYear(int yearIn)
{
year = yearIn;
}
string getColor()
{
return color;
}
void setColor(string colorIn)
{
color = colorIn;
}
void printCarInfo()
{
cout << "Make: " << make <<endl;
cout << "Model: " << model <<endl;
cout << "Year: " << year << endl;
cout << "Color: " << color << endl;
13

}
};

int main()
{
Car car;
car.setMake("Toyota");
car.setModel("Corolla");
car.setYear(2018);
car.setColor("Blue");
car.printCarInfo();
return 0;
}

OUTPUT
14

LAB TASK 5
#include <iostream>
Using namespace std;
class BankAccount
{
private:
int accountNumber;
double balance;
double interestRate;
public:
int getAccountNumber()
{
return accountNumber;
}
void setAccountNumber(int accountNumberIn)
{
accountNumber = accountNumberIn;
}
double getBalance()
{
return balance;
}
void setBalance(double balanceIn)
{
balance = balanceIn;
15

}
double getInterestRate()
{
return interestRate;
}
void setInterestRate(double interestRateIn)
{
interestRate = interestRateIn;
}
double calculateInterest()
{
return balance * interestRate;
}
};

int main()
{
BankAccount account;
account.setAccountNumber(123456);
account.setBalance(10000);
account.setInterestRate(0.05);
cout << "Interest earned: " << account.calculateInterest() <<endl;
return 0;
}
16

OUTPUT

LAB TASK 6
#include <iostream>
using namespace std;
class Time
{
private:
int hour;
int minute;
int second;

public:
int getHour()
{
return hour;
}
void setHour(int h)
{
if (h >= 0 && h < 12)
{
17

hour = h;
}
else if (h >= 12 && h < 24)
{
hour = h - 12;
}
}
int getMinute()
{
return minute;
}
void setMinute(int m)
{
if (m >= 0 && m < 60)
{
minute = m;
}
}
int getSecond()
{
return second;
}
void setSecond(int s)
{
if (s >= 0 && s < 60)
18

{
second = s;
}
}
void print12HourFormat()
{
cout << hour << ":" << minute << ":" << second;
if (hour < 12)
{
cout << " AM";
}
else {
cout << " PM";
}
}
};

int main()
{
Time t;
t.setHour(16);
t.setMinute(30);
t.setSecond(0);
t.print12HourFormat();
return 0;
19

OUTPUT

You might also like