0% found this document useful (0 votes)
3 views

Assignme

The document contains an assignment for a course in Object Oriented Programming at the University of Lahore, detailing five programming tasks. These tasks include counting characters and vowels from a file, implementing structures and classes with binary I/O, demonstrating inheritance and virtual functions, and handling exceptions in C++. Each task is accompanied by code snippets that illustrate the required functionality.

Uploaded by

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

Assignme

The document contains an assignment for a course in Object Oriented Programming at the University of Lahore, detailing five programming tasks. These tasks include counting characters and vowels from a file, implementing structures and classes with binary I/O, demonstrating inheritance and virtual functions, and handling exceptions in C++. Each task is accompanied by code snippets that illustrate the required functionality.

Uploaded by

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

The University of Lahore

Department of CS & IT
Assignment 04
Instructor Name: Miss Iqra Adnan
Course Title: Object Oriented Programming
Student Name:Arooj University ID: 70146896

Question 1: Q1: Count characters and vowels from a file


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

int main() {
ifstream f("text.txt");
char c;
int ch = 0, v = 0;
while (f.get(c)) {
ch++;
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' ||
c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U') {
v++;
}
}
cout << "Characters: " << ch << endl;
cout << "Vowels: " << v << endl;
return 0;
}

Q2: Structure with binary I/O


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

struct Customer {
int AccountNumber;
char Name[50];
float Amount;
};

int main() {
Customer c[3];
for (int i = 0; i < 3; i++) {
cin >> c[i].AccountNumber >> c[i].Name >> c[i].Amount;
}
ofstream out("Bank.txt", ios::binary);
out.write((char*)&c, sizeof(c));
out.close();
return 0;
}

Q3: Class with binary I/O


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

class Country {
public:
int id;
char Name[50];
void get() {
cout<<"Enter id: ";
cin >> id ;
cout<<"Ente Name: ";cin>>Name;
}
void show() {
cout << id << " " << Name << endl;
}
};

int main() {
Country c;
c.get();
ofstream out("country.txt", ios::binary);
out.write((char*)&c, sizeof(c));
out.close();

Country d;
ifstream in("country.txt", ios::binary);
in.read((char*)&d, sizeof(d));
d.show();
in.close();
return 0;
}
Q4: Inheritance + Virtual Function
#include <iostream>
using namespace std;

class BankAccount {
public:
int AccountNumber;
float balance;
BankAccount(int a, float b) {
AccountNumber = a;
balance = b;
}
virtual void displayBalance() {
cout << balance << endl;
}
void deposit(float x) {
balance += x;
}
void withdraw(float x) {
balance -= x;
}
};

class SavingsAccount : public BankAccount {


public:
float interestRate;
SavingsAccount(int a, float b, float i) : BankAccount(a, b) {
interestRate = i;
}
void displayBalance() {
cout << balance << " " << interestRate << endl;
}
};

int main() {
BankAccount *b;
SavingsAccount s(1, 1000, 0.05);
b = &s;
b->displayBalance();
return 0;
}

Q5: Exception handling


#include <iostream>
using namespace std;

float CalculateArea(float c1, float c2) {


if (c1 < 0 || c2 < 0)
throw "Invalid Value: Area can't be negative.";
if (c1 > 1000 || c2 > 1000)
throw "Invalid Value: Above 1000 sqkm is not acceptable.";
return c1 * c2;
}

int main() {
float x, y;
cin >> x >> y;
try {
float area = CalculateArea(x, y);
cout << area << endl;
} catch (const char* msg) {
cout << msg << endl;
}
return 0;

You might also like