Last active
November 5, 2020 02:57
-
-
Save lablnet/e5b928884353ecc127136a4f774b8f37 to your computer and use it in GitHub Desktop.
Lab 03
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
class Rectangle { | |
private: | |
double length; | |
double width; | |
bool error; | |
public: | |
Rectangle() { | |
this->length = 1; | |
this->width = 1; | |
} | |
double getLength() { | |
return length; | |
} | |
double getWidth() { | |
return width; | |
} | |
bool isError() { | |
return this->error; | |
} | |
Rectangle setLength(double length) { | |
this->error = false; | |
if (length >= 0 && length < 20 && length == (double) length) { | |
this->length = length; | |
} else { | |
this->error = true; | |
std::cout << "Please enter between 0 to 20, and should be float"; | |
} | |
return *this; | |
} | |
Rectangle setWidth(double width) { | |
this->error = false; | |
if (width >= 0 && width < 20 && width == (double) width) { | |
this->width = width; | |
} else { | |
this->error = true; | |
std::cout << "Please enter between 0 to 20, and should be float"; | |
} | |
return *this; | |
} | |
void calculatePerimeter() { | |
std::cout << "Perimeter of rectangle is: " << 2 * (this->length + this->width) << std::endl; | |
} | |
void calculateArea() { | |
std::cout << "Area of rectangle is: " << (this->length * this->width) << std::endl; | |
} | |
}; | |
int main() { | |
Rectangle r = Rectangle(); | |
double l, w; | |
while (true) { | |
std::cout << "Enter length: "; | |
std::cin >> l; | |
std::cout << "Enter width: "; | |
std::cin >> w; | |
r.setWidth(w).setLength(l); | |
if (!r.isError()) break; | |
else { | |
std::cout << "Try again."; | |
} | |
} | |
r.calculateArea(); | |
r.calculatePerimeter(); | |
return 0; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
class Invoice { | |
private: | |
double price; | |
int num; | |
int quantity; | |
std::string desc; | |
public: | |
Invoice(double price, int num, int quantity, const std::string &desc) : price(price), num(num), quantity(quantity), | |
desc(desc) {} | |
double getPrice() const { | |
return price; | |
} | |
void setPrice(double price) { | |
Invoice::price = price; | |
} | |
int getNum() const { | |
return num; | |
} | |
void setNum(int num) { | |
Invoice::num = num; | |
} | |
int getQuantity() const { | |
return quantity; | |
} | |
void setQuantity(int quantity) { | |
Invoice::quantity = quantity; | |
} | |
const std::string &getDesc() const { | |
return desc; | |
} | |
void setDesc(const std::string &desc) { | |
Invoice::desc = desc; | |
} | |
double getInvoiceAmount() { | |
double res = this->quantity * this->price; | |
if (res < 0 ) return 0; | |
return res; | |
} | |
}; | |
int main() { | |
Invoice i = Invoice(150, 645745, 6, "Sold"); | |
std::cout << i.getInvoiceAmount(); | |
return 0; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
class Data { | |
int month, day, year; | |
public: | |
Data(int month, int day, int year) : month(month), day(day), year(year) {} | |
int getMonth() const { | |
return month; | |
} | |
void setMonth(int month) { | |
Data::month = month; | |
} | |
int getDay() const { | |
return day; | |
} | |
void setDay(int day) { | |
Data::day = day; | |
} | |
int getYear() const { | |
return year; | |
} | |
void setYear(int year) { | |
Data::year = year; | |
} | |
void displayDate() { | |
std::cout << this->day << "/" << month << "/" << this->year; | |
} | |
}; | |
int main() { | |
Data d = Data(6, 16, 2020); | |
d.displayDate(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
class Employee { | |
std::string fName; | |
std::string lName; | |
double salary; | |
public: | |
Employee(const std::string &fName, const std::string &lName, double salary) { | |
this->fName = fName; | |
this->lName = lName; | |
this->salary = (salary > 0) ? salary : 0.0; | |
} | |
const std::string &getFName() const { | |
return fName; | |
} | |
void setFName(const std::string &fName) { | |
Employee::fName = fName; | |
} | |
const std::string &getLName() const { | |
return lName; | |
} | |
void setLName(const std::string &lName) { | |
Employee::lName = lName; | |
} | |
double getSalary() const { | |
return salary; | |
} | |
void setSalary(double salary) { | |
this->salary = (salary > 0) ? salary : 0.0; | |
} | |
}; | |
int main() { | |
Employee e1 = Employee("Ali", "Khan", 26656); | |
std::cout << e1.getFName() << " " << e1.getSalary() << std::endl; | |
e1.setSalary((e1.getSalary() / 100) * 10); | |
std::cout << e1.getFName() << " " << e1.getSalary() << std::endl; | |
Employee e2 = Employee("Usman", "Khan", 36656); | |
std::cout << e2.getFName() << " " << e2.getSalary() << std::endl; | |
e1.setSalary((e1.getSalary() / 100) * 10); | |
std::cout << e2.getFName() << " " << e2.getSalary() << std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment