Assignment No 1 Oop
Assignment No 1 Oop
Assignment # 1
Date of Submission:
28th September 2023
Prepared For:
Ma’am Asma Bibi
Section:
BEE-3C
Prepared by:
#include <iostream>
using namespace std;
class Point
{
private:
double x;
double y;
public:
Point()
{
x = 0.0;
y = 0.0;
}
Point(double A, double B)
{
x =A;
y = B;
}
double getX() const
{
return x;
}
double getY() const
{
return y;
}
void setX(double newX)
{
x = newX;
}
void setY(double newY)
{
y = newY;
}
void display()
2. Create a class called BankAccount that models a checking account at a bank. The
program creates an account with an opening balance, displays the balance, makes a
deposit and a withdrawal, and then displays the new balance. Note in withdrawal
function, if balance is below Rs. 500 then display message showing insufficient balance
otherwise allow withdrawal.
#include <iostream>
using namespace std;
class BankAccount {
private:
double balance;
public:
BankAccount(double initialBalance)
{
balance = 10500;
}
void displayBalance() const
{
cout << "Current Balance: Rs. " << balance << endl;
}
void deposit(double amount)
{
if (amount > 0) {
balance += amount;
cout << "Deposited Rs. " << amount << endl;
int main() {
BankAccount account(1000.0);
account.displayBalance();
account.deposit(500.0);
account.displayBalance();
account.withdraw(800.0);
account.displayBalance();
account.withdraw(700.0);
account.displayBalance();
return 0;
}
#include <iostream>
#include <string>
using namespace std;
#include <iostream>
#include <string>
using namespace std;
int main() {
int numUsers;
cout << "Enter the number of users: ";
cin >> numUsers;
cout << "Enter the name of user " << i + 1 << ": ";
cin >> userName;
cout << "Enter the number of units consumed by " << userName << ": ";
cin >> unitsConsumed;
cout << "Charges for " << userName << ": Rs. " << charges << endl;
}
return 0;
}
4. Develop a C++ program find the largest among three different numbers entered by
user.
#include <iostream>
using namespace std;
int main()
{
double num1, num2, num3;
cout << "Enter three different numbers: ";
cin >> num1 >> num2 >> num3;
if (num1 != num2 && num1 != num3 && num2 != num3)
{
if (num1 > num2 && num1 > num3)
{
cout << "The largest number is: " << num1 << endl;
}
else if (num2 > num1 && num2 > num3)
{
cout << "The largest number is: " << num2 << endl;
}
else
{
cout << "The largest number is: " << num3 << endl;
}
}
return 0;
}
#include <iostream>
using namespace std;
int main()
{
double totalMarks, obtainedMarks;
cout << "Enter the total marks: ";
cin >> totalMarks;
cout << "Enter the obtained marks: ";
cin >> obtainedMarks;
#include <iostream>
using namespace std;
class Geometry {
private:
double length;
double width;
public:
Geometry(double l, double w)
{
length = l;
width = w;
}
double squareArea()
{
return length * length;
}
double squarePerimeter()
{
return 4 * length;
}
double rectangleArea()
{
return length * width;
}
double rectanglePerimeter()
{
return 2 * (length + width);
}
};
int main()
{
double length, width;
cout << "Enter the length: ";
if (length == width)
{
cout << "It's a square." << endl;
cout << "Area: " << geometry.squareArea() << endl;
cout << "Perimeter: " << geometry.squarePerimeter() << endl;
}
else
{
cout << "It's a rectangle." << endl;
cout << "Area: " << geometry.rectangleArea() << endl;
cout << "Perimeter: " << geometry.rectanglePerimeter() << endl;
}
return 0;
}
7. Answer the questions (i) and (iii) after going through the following class:
class Seminar
{
int time;
public:
Seminar() //Function 1
{
time = 30;
cout << "Seminar starts now" << endl;
}
void lecture() //Function 2
{
cout << "Lectures in the seminar on" << endl;
}
Seminar(int duration) //Function 3
a. Write statements in C++ that would execute Function 1 and Function 3 of class
Seminar.
Seminar seminar1;
Seminar seminar2(45);
public:
Test () // Function 1
{
strcpy (paper, "Computer");
Test test1
Test test2("Biology");
Test test3(75);
Test test4("Maths", 92);
Constructor Overloading.
class Sample
{
private:
int x;
double y;
public :
Sample(); //Constructor 1
Sample(int); //Constructor 2
Sample(int, int); //Constructor 3
Sample(int, double); //Constructor 4
};
a. Write the definition of the constructor 1 so that the private member variables are
initialized to 0.
Sample::Sample()
{
x = 0;
y = 0.0;
}
b. Write the definition of the constructor 2 so that the private member variable x is
initialized according to the value of the parameter, and the private member variable
y is initialized to 0.
Sample::Sample(int value)
{
x = value;
y = 0.0;
}
For Constructor 3: -
For Constructor 4: -
10. Find the output of given Program and also find error if it has. Write down description
of every statement having //.
#include<iostream>//
using namespace std ;//
class Box //
{
public : //
double length ; //
double breadth ; //
double height ; //
};
int main (void) //
{
Box Box1 ; //
Box Box2 ; //
double volume = 0.0 ; //
Box1.height = 18.0 ; //
Box1.length = 78.0 ; //
Box1.breadth = 24.0 ; //
Box2.height = Box1.height − 10 ; //
Box2.length = Box1.length / 2 . 0 ; //
Box2.breadth = 0.2 5∗ Box1.length ; //
volume = Box1.height ∗ Box1 . length ∗ Box1 . breadth ; //
Errors in Code
OUTPUT
1. #include <iostream>: Includes the header file for input and output stream handling.
2. using namespace std;: Specifies that the program will use the std namespace.
3. class Box {};: Defines a class named Box with three double precision member variables:
length, breadth, and height.
4. int main() {: Start of the main function.
5. Box Box1; and Box Box2;: Declares two objects of the Box class named Box1 and Box2.
6. double volume = 0.0;: Declares a double variable volume and initializes it to 0.0.
7. Setting values for the dimensions of Box1 using the dot operator.
8. Calculations to set values for the dimensions of Box2.
9. Calculation of the volume of Box1.
10. cout << endl << "Volume of Box1 = " << volume;: Outputs the volume of Box1.
11. cout << endl << "Box2 has sides which total " ...: Outputs the total of sides of Box2.
12. cout << endl << "A Box object occupies " ...: Outputs the size of Box1 in bytes.
13. return 0;: Indicates the end of the main function.