0% found this document useful (0 votes)
4 views16 pages

Oopassignment

The document contains solutions for an Object-Oriented Programming assignment from the University of Poonch Rawalakot, including code examples for various programming tasks. It covers classes for handling phone numbers, time manipulation, account management, and shapes with their respective areas. Each section includes code snippets and explanations for the implemented functionality.

Uploaded by

mahagab502
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)
4 views16 pages

Oopassignment

The document contains solutions for an Object-Oriented Programming assignment from the University of Poonch Rawalakot, including code examples for various programming tasks. It covers classes for handling phone numbers, time manipulation, account management, and shapes with their respective areas. Each section includes code snippets and explanations for the implemented functionality.

Uploaded by

mahagab502
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

UNIVERSITY OF

POONCH RAWALAKOT

DEPARTMENT OF COMPUTER SCIENCE & INFORMATION TECHNOLOGY

ASSIGNMENT NO # 02

Course Title: Object-Oriented Programming

Course Code: CS-1201

Submitted To: Mam kanwal Mehmoud

Submitted By :

Name Roll NO
Abdul Moiz 02

Semester: BSCS2nd

Session: 2024-2028

Date of submission: 10-10-2025


Solution for QuestionNo 1

#include <iostream>
#include <string>
using namespace std;

class LocalPhone {
protected:
string phone;

public:
void inputPhone() {
cout << "Enter local phone number: ";
getline(cin, phone);
}

void displayPhone() {
cout << "Phone Number: " << phone << endl;
}
};

class NatPhone : public LocalPhone {


protected:
string cityCode;

public:
void inputCityCode() {
cout << "Enter city code: ";
getline(cin, cityCode);
}

void showCityCode() {
cout << "City Code: " << cityCode << endl;
}
};

class IntPhone : public NatPhone {


protected:
string countryCode;

public:
void inputCountryCode() {
cout << "Enter country code: ";
getline(cin, countryCode);
}
void showCountryCode() {
cout << "Country Code: " << countryCode << endl;
}

void displayAll() {
cout << "\n----- Complete Phone Information -----\n";
showCountryCode();
showCityCode();
displayPhone();
}
};

int main() {
IntPhone phone1;

cout << "Enter complete phone information:\n";


[Link]();
[Link]();
[Link]();

[Link]();

return 0;
}

Output for Question 1

Solution for QuestionNO 2


#include <iostream>
using namespace std;

class Time {
private:
int hour;
int minute;
int second;

public:

Time(int h, int m, int s) {


hour = h;
minute = m;
second = s;
}

void show() {
cout << "Time: " << hour << ":" << minute << ":" << second << endl;
}

void operator++() {
minute = minute + 1;
if (minute == 60) {
minute = 0;
hour = hour + 1;
if (hour == 24)
hour = 0;
}
}

void operator--() {
minute = minute - 1;
if (minute < 0) {
minute = 59;
hour = hour - 1;
if (hour < 0)
hour = 23;
}
}
};

int main() {
Time t1(10, 59, 30);
cout << "Initial time: ";
[Link]();

++t1;
cout << "After ++ : ";
[Link]();

--t1;
cout << "After -- : ";
[Link]();

return 0;
}

Output for Question No 2

Solution for Question No 3


#include <iostream>

#include <cmath>
using namespace std;

class Account {
protected:
double balance;

public:

Account(double initialBalance) {
if (initialBalance >= 0.0)
balance = initialBalance;
else {
balance = 0.0;
cout << "Error: Initial balance invalid. Balance set to 0.0" << endl;
}
}
void Credit(double amount) {
balance += amount;
}

void Debit(double amount) {


if (amount > balance)
cout << "Debit amount exceeded account balance." << endl;
else
balance -= amount;
}

double getBalance() const {


return balance;
}
};

class SavingsAccount : public Account {


private:
double interestRate;

public:

SavingsAccount(double initialBalance, double rate)


: Account(initialBalance) {
interestRate = rate;
}

double calculateInterest(int years) {


double interest = balance * pow((1 + interestRate), years);
return interest - balance;
}
};

class CheckingAccount : public Account {


private:
double fee;

public:

CheckingAccount(double initialBalance, double feeAmount)


: Account(initialBalance) {
fee = feeAmount;
}
void Credit(double amount) {
balance += amount;
balance -= fee;
}

void Debit(double amount) {


if (amount + fee > balance)
cout << "Debit amount exceeded account balance." << endl;
else
balance -= (amount + fee);
}
};

int main() {
cout << "----- Account Hierarchy Demonstration -----" << endl;

int choice;
cout << "Select Account Type:" << endl;
cout << "1. Savings Account" << endl;
cout << "2. Checking Account" << endl;
cout << "Enter your choice: ";
cin >> choice;

if (choice == 1) {
double balance, rate;
int years;

cout << "\nEnter initial balance: ";


cin >> balance;
cout << "Enter yearly interest rate (e.g., 0.05 for 5%): ";
cin >> rate;

SavingsAccount s(balance, rate);

cout << "Enter number of years to calculate interest: ";


cin >> years;

double interest = [Link](years);


cout << "\nInterest earned in " << years << " years = " << interest << endl;
cout << "Balance after interest = " << [Link]() + interest << endl;

double creditAmount, debitAmount;


cout << "\nEnter amount to deposit: ";
cin >> creditAmount;
[Link](creditAmount);
cout << "Enter amount to withdraw: ";
cin >> debitAmount;
[Link](debitAmount);

cout << "Final balance = " << [Link]() << endl;


}

else if (choice == 2) {
double balance, fee;

cout << "\nEnter initial balance: ";


cin >> balance;
cout << "Enter transaction fee: ";
cin >> fee;

CheckingAccount c(balance, fee);

double creditAmount, debitAmount;


cout << "Enter amount to deposit: ";
cin >> creditAmount;
[Link](creditAmount);

cout << "Enter amount to withdraw: ";


cin >> debitAmount;
[Link](debitAmount);

cout << "Final balance = " << [Link]() << endl;


}

else {
cout << "Invalid choice." << endl;
}

return 0;
}

Output for Question 3


Solution for question No 4
Main Menu form with different button

Code for main forms buttons

namespace ShapesApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void btnRectangle_Click(object sender, EventArgs e)


{
RectangleForm frm = new RectangleForm();
[Link]();
[Link]();
}

private void btnTrapezium_Click(object sender, EventArgs e)


{
TrapeziumForm frm = new TrapeziumForm();
[Link]();
[Link]();
}

private void btnSquare_Click(object sender, EventArgs e)


{
SquareForm frm = new SquareForm();
[Link]();
[Link]();
}

private void btnParallelogram_Click(object sender, EventArgs e)


{
ParallelogramForm frm = new ParallelogramForm();
[Link]();
[Link]();
}

private void btnCircle_Click(object sender, EventArgs e)


{
CircleForm frm= new CircleForm();
[Link]();
[Link]();
}

private void btnTriangle_Click(object sender, EventArgs e)


{
TriangleForm frm = new TriangleForm();
[Link]();
[Link]();
}

Output for Question NO 4


Rectangle form

Parallelogram
Form
Square Form

Triangle Form
Circle Form

Trapezium Form
Base class
public class Shapes
{
protected double length, width, height, radius, baseValue;

public void setLength(double l) {


length = l;
}
public double getLength() {
return length;
}

public void setWidth(double w) {


width = w;
}
public double getWidth() {
return width;
}

public void setHeight(double h) {


height = h;
}
public double getHeight() {
return height;
}

public void setRadius(double r) {


radius = r;
}
public double getRadius() {
return radius;
}

public void setBaseValue(double b) {


baseValue = b;
}
public double getBaseValue() {
return baseValue;
}
}

}
Derived classes
namespace ShapesApp1
{
public class Circle : Shapes
{
public double GetArea()
{
return 3.14 * radius * radius;
}
}

namespace ShapesApp1
{
public class Parallelogram : Shapes
{

public double GetArea()


{
return getBaseValue() * getHeight();
}
}

namespace ShapesApp1
{

public class Rectangle : Shapes


{
public double GetArea()
{
return getLength() * getWidth();
}
}

namespace ShapesApp1
{

public class Square : Shapes


{
public double GetArea()
{

return getLength() * getLength();


}
}

namespace ShapesApp1
{
public class Trapezium : Shapes
{

public double GetArea()


{

return 0.5* (getBaseValue() + getWidth()) * getHeight() ;


}
}

namespace ShapesApp1
{
public class Triangle : Shapes
{
public double GetArea()
{
return 0.5 * getBaseValue() * getHeight();
}
}

You might also like