Oop 3
Oop 3
//Methods
double CalculateArea()const{
double area;
return (area = length * width);
}
rectangle differently (e.g., one with default values and another with
specific values).
return 0;
}
#include <iostream>
using namespace std;
class Rectangle {
private:
double length;
double width;
public:
//Setter Method
void setLength(double length) {
this->length = length;
}
void setWidth(double width) {
this->width = width;
}
//Getter Methods
double getLength() const {
return length;
}
double getWidth() const {
return width;
}
double CalculateArea()const{
double area;
return (area = length * width);
}
void DisplayArea() const{
cout << "Area = " << CalculateArea() << endl;
}
};
int main() {
Rectangle rec1;
rec1.setLength(5);
rec1.setWidth(10);
cout << "Length: " << rec1.getLength() << ", Width: " << rec1.getWidth() <<
endl;
cout << "Area = " << rec1.CalculateArea() << endl;
return 0;
}
//Getter Methods
double getLength() const {
return length;
}
double getWidth() const {
return width;
}
double CalculateArea()const{
return length * width;
}
void DisplayArea() const{
cout << "Area = " << CalculateArea() << endl;
}
};
int main() {
Rectangle rec1(10);
cout << "Length: " << rec1.getLength() << ", Width: " << rec1.getWidth() <<
endl;
cout << "Area = " << rec1.CalculateArea() << endl;
return 0;
}
//Methods
double CalculateArea()const{
return length * width;
}
void DisplayArea() const{
cout << "Area = " << CalculateArea() << endl;
}
};
int main() {
Rectangle rectangles[3] = {
Rectangle(5, 3),
Rectangle(6, 2),
Rectangle(3, 4)
};
return 0;
}
//parameterized constructor
Rectangle(double l, double w){
this->length = length;
this->width = width;
}
};
int main() {
Rectangle r1;
Rectangle r1(5, 7);
return 0;
}
};
int main() {
Rectangle r1;
r1.DisplayArea();
return 0;
}
class accessSpecifiers {
private:
int privateVar; //Can't be accessed outside the class directly.
public:
int publicVar; //Can be accessed anywhere.
//Methods
void setPrivateVar(int privateVar) {
this->privateVar = privateVar;
}
void display() const {
cout << "PrivateVar = " << privateVar << endl;
}
};
int main() {
accessSpecifiers test;
test.setPrivateVar(10);
test.display();
return 0;
}
//Getter Methods
double getLength() const {
return length;
}
double getWidth() const {
return width;
}
double CalculateArea()const{
return length * width;
}
void DisplayArea() const{
cout << "Area = " << CalculateArea() << endl;
}
};
int main() {
Rectangle r1;
r1.setLength(-5);
r1.setWidth(5);
cout << "Length: " << r1.getLength() << ", Width: " << r1.getWidth() << endl;
r1.DisplayArea();
return 0;
}
//Setter Methods
void setLength() { //setting length for rectangle
cout << "Enter the length for Rectangle: ";
cin >> length;
}
void setWidth() { //setting width for rectangle
cout << "Enter the width for Rectangle: ";
cin >> width;
}
//Getter Methods
double getLength() const{ //Getting Length
return length;
}
double getWidth() const{ //Getting Width
return width;
}
void Menu() {
cout << "\n1. Create a Rectangle\n";
cout << "2. Display the area of Rectangle\n";
cout << "3. Update the Dimentions\n";
cout << "4. Exit\n";
}
int main() {
Rectangle rec1;
int choice;
while (true) {
Menu();
switch (choice) {
case 1:
rec1.createRectangle();
break;
case 2:
rec1.displayArea();
break;
case 3:
rec1.updateDimentions();
break;
case 4:
cout << "\n\nExiting Program... ... ... !!\n\n";
exit(0);
}
cout << "\n\n";
}
return 0;
}