0% found this document useful (0 votes)
20 views9 pages

Oop 3

Uploaded by

Awais Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views9 pages

Oop 3

Uploaded by

Awais Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

1.

Create a Rectangle Class

Implement a Rectangle class that includes:

o Private member variables for length and width.

o A default constructor that initializes the rectangle to default values

(e.g., length and width set to 0).

o A parameterized constructor that accepts values for length and width.

o Implement a method to calculate and display the area of the rectangle.


class Rectangle {
private:
double length;
double width;
public:
//default constructor
Rectangle() {
length = 0;
width = 0;
}
//parameterised constructor
Rectangle(double length, double width) {
this->length = length;
this->width = width;
}

//Methods
double CalculateArea()const{
double area;
return (area = length * width);
}

void DisplayArea() const{


cout << "Area = " << CalculateArea() << endl;
}
};

2. Demonstrate Constructor Overloading

Extend the Rectangle class to include:

o Constructor overloading with multiple constructors that initialize the

rectangle differently (e.g., one with default values and another with

specific values).

o Provide examples in a main program demonstrating how to create

Rectangle objects using different constructors.


#include <iostream>
using namespace std;
class Rectangle {
private:
double length;
double width;
public:
//default constructor
Rectangle() {
length = 0;
width = 0;
}
//one parameter constructor
Rectangle(int length) {
this->length = length;
this->width = length;
}
//parameterised constructor
Rectangle(double length, double width) {
this->length = length;
this->width = width;
}
};
int main() {
//by using default constructor.
Rectangle rec1;
//by using one parameter constructor.
Rectangle rec2(5);
//by using two parameter constructor.
Rectangle rec3(2, 6);

return 0;
}

3. Use of Setter and Getter Methods


o Implement setter and getter methods in the Rectangle class for
length and width.
o Write a main program that creates a Rectangle object, modifies its
dimensions using setters, and displays the updated area using getters.

#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;
}

4. Initialize Using Initializer Lists


o Modify the Rectangle class to use initializer lists in the constructors.
o Show an example in the main program where an object is initialized
using the initializer list.
#include <iostream>
using namespace std;
class Rectangle {
private:
double length;
double width;
public:
//default constructor initialiser list
Rectangle(): length(0), width(0) {}

//one parameter constructor initialiser list


Rectangle(int length): length(length), width(length) {}

//parameterised constructor initialiser list


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

//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;
}

5. Array of Rectangle Objects


o Write a program that creates an array of Rectangle objects.
o Initialize the array with different lengths and widths using
constructors.
o Iterate through the array and display the area of each rectangle using a
simple for loop.
#include <iostream>
using namespace std;
class Rectangle {
private:
double length;
double width;
public:
//default constructor initialiser list
Rectangle(): length(0), width(0) {}

//one parameter constructor initialiser list


Rectangle(int length): length(length), width(length) {}

//parameterised constructor initialiser list


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

//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)
};

//Displaying Area of rectangles


for (int i = 0; i < 3; i++) {
cout << "RECTANGLE-0" << i + 1 << "\n";
rectangles[i].DisplayArea();
}

return 0;
}

6. Default Constructor and Parameterized Constructor

o Discuss what happens when both default and parameterized


constructors are defined.
o Create a simple example illustrating the usage of both in a single
program.
#include <iostream>
using namespace std;
class Rectangle {
private:
double length;
double width;
public:
//default constructor
Rectangle(){
length = 0;
width = 0;
}

//one parameter constructor


Rectangle(int length){
this->length = length;
this->width = length;
}

//parameterized constructor
Rectangle(double l, double w){
this->length = length;
this->width = width;
}

};
int main() {
Rectangle r1;
Rectangle r1(5, 7);

return 0;
}

7. Default Constructor with Default Arguments


o Implement a default constructor that includes default arguments for
length and width.
o Create instances of the Rectangle class to demonstrate the use of
default arguments.
#include <iostream>
using namespace std;
class Rectangle {
private:
double length;
double width;
public:
//Default constructor with default arguments
Rectangle(double l = 0, double w = 0) {
length = l;
width = w;
}
//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.DisplayArea();
return 0;
}

8. Understanding the Purpose of Access Specifiers


o Create a class that illustrates the use of public, private, and
protected access specifiers.
o Write methods that demonstrate how each access specifier affects
accessibility of member variables.
#include <iostream>
using namespace std;

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();

test.publicVar = 20; //setting publicVar outside the class explicitly.


cout << "PublicVar = " << test.publicVar << endl;

return 0;
}

9. Implement Basic Input Validation


o Add basic input validation in the setter methods of the Rectangle
class to ensure that negative values for dimensions are not accepted.
o Modify the main program to handle these validations gracefully.
#include <iostream>
using namespace std;
class Rectangle {
private:
double length;
double width;
public:
//Default constructor with default arguments
Rectangle(double l = 0, double w = 0) {
length = l;
width = w;
}
//Setter Methods
void setLength(double length) {
if (length > 0) {
this->length = length;
}
else
cout << "Invalid input" << endl;
}
void setWidth(double width) {
if (width > 0) {
this->width = width;
}
else
cout << "Invalid input" << endl;
}

//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;
}

10.Simple Text-Based Menu System


o Create a simple text-based menu in the main program that allows
users to:
 Create a rectangle.
 Display the area of the rectangle.
 Modify dimensions using setters.
 Exit the program.
o Use a loop to keep the program running until the user chooses to exit.
#include <iostream>
using namespace std;
class Rectangle {
private:
double length;
double width;
public:
//Default Constructor
Rectangle(double length = 0, double width = 0): length(length), width(width)
{}

//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;
}

//Some other functions


void createRectangle() {
setLength();
setWidth();
}
void displayArea() const{
cout << "Area = " << getLength() << " * " << getWidth() << " = " <<
(getLength() * getWidth()) << endl;
}
void updateDimentions() {
setLength();
setWidth();
}
};

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();

cout << "Enter your choice: ";


cin >> choice;

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;
}

You might also like