Uploaded by Fatemah Hatem

some beginner codes

advertisement
Lab 9 Fatimah Hatem Elsayed 2021/10731
Exercise 1:
#include <iostream>
using namespace std;
class rectangle {
private :
double height, width;
public :
rectangle() {
height = 1.0, width = 1.0;
}
rectangle(double h, double w)
{
height = h, width = w;
}
double getPer()
{
return (2*(height)+2*(width));
}
double getArea() {
return height * width;
}
};
int main()
{
int l, w;
cout << "Enter length and width: ";
cin >> l >> w;
rectangle r(l,w);
cout << "Area: " << r.getArea();
cout << "\nPerimeter: " << r.getPer();
}
Exercise 2:
#include <iostream>
#include <fstream>
using namespace std;
class Fan
{
private:
int speed; bool on; double radius;
public:
Fan()
{
speed = 1, on = false, radius = 5.0;
}
void setSpeed(int s)
{
this->speed = s;
}
void setRadius(double r)
{
this->radius = r;
}
void setCondition(bool o)
{
this->on = o;
}
int getSpeed()
{
return speed;
}
double getRadius()
{
return radius;
}
bool getCondition()
{
return on;
}
};
int main()
{
Fan fan1, fan2;
int s1 = 3, s2 = 2;
double r1 = 10, r2 = 5;
bool con1 = true, con2 = false;
fan1.setSpeed(s1), fan1.setRadius(r1), fan1.setCondition(con1);
cout << "Fan 1:\n" << "Speed: " << fan1.getSpeed() << "\nRadius: " << fan1.getRadius() << "\nCondtion: ";
if (fan1.getCondition() == true)
{
cout << "on";
}
else
{
cout << "off";
}
fan2.setSpeed(s2), fan2.setRadius(r2), fan2.setCondition(con2);
cout << "\nFan 2:\n" << "Speed: " << fan2.getSpeed() << "\nRadius: " << fan2.getRadius() << "\nCondtion: ";
if (fan2.getCondition() == true)
{
cout << "on";
}
else
{
cout << "off";
}
}
Exercise 3:
#include <iostream>
using namespace std;
class Account {
private:
int id;
double bal;
double annualInterestRate;
public:
Account() {
id = 0, bal = 0; annualInterestRate = 0;
}
double getMonthlyInterestRate(double annualInterestRate) {
return annualInterestRate / 12;
}
bool setid(int id)
{
this->id = id;
return true;
}
bool setbal(double bal)
{
this->bal = bal;
return true;
}
bool setint(double annualInterestRate) {
this->annualInterestRate = annualInterestRate;
return true;
}
double withdraw(int w) {
return bal - w;
}
double deposit(int d) {
return bal + d;
}
};
int main()
{
int id;
double bal, i,w,d;
Account r;
cout << "Enter ID: ";
cin >> id;
r.setid(id);
cin >> bal;
r.setbal(bal);
i = 4.5;
cout << "\nYour annual interest rate is: "<< r.setint(i);
cout << "\nEnter the amount you wish to withdraw: ";
cin >> w;
cout << "Your new balance is: " << r.withdraw(w);
cout << "\nEnter the amount you want to deposit: ";
cin >> d;
cout << "Your balance is now: " << r.deposit(d);
}
Exercise 4:
#include <iostream>
#include <fstream>
using namespace std;
class MyPoint
{
private:
double x, y;
public:
MyPoint() { x = 0, y = 0; };
MyPoint(double x, double y)
{
this->x = x;
this->y = y;
}
void setX(double x)
{
this->x = x;
}
double getX()
{
return x;
}
void sety(double y)
{
this->y = y;
}
double gety()
{
return y;
}
void distance(double x, double y)
{
cout << "(" << (x - this->x) << "," << (y - this->y)<< ")";
}
};
int main()
{
MyPoint p1;
double x = 0, x1 = 10, y = 0, y1 = 30.5;
p1.setX(x), p1.sety(y);
p1.distance(x1, y1);
}
Download