C++ Programming Assignment
C++ Programming Assignment
#include <iostream>
using namespace std;
public:
void setDetails(string n, int a) {
name = n;
age = a;
}
void displayDetails() {
cout << "Name: " << name << ", Age: " << age << endl;
}
};
// Inheritance
class Student : public Person {
public:
void study() {
cout << "Student is studying." << endl;
}
};
// Polymorphism
class Shape {
public:
virtual void draw() {
cout << "Drawing a shape." << endl;
}
};
class Circle : public Shape {
public:
void draw() override {
cout << "Drawing a circle." << endl;
}
};
int main() {
Student s;
s.setDetails("Temwa", 20);
s.displayDetails();
s.study();
Shape* shape;
Circle circle;
shape = &circle;
shape->draw();
return 0;
}
#include <iostream>
using namespace std;
int main() {
calculateArea(5, 4);
return 0;
}
class Rectangle {
private:
float length, width;
public:
Rectangle(float l, float w) : length(l), width(w) {}
void calculateArea() {
cout << "Area: " << length * width << endl;
}
};
int main() {
Rectangle r(5, 4);
r.calculateArea();
return 0;
}
#include <iostream>
using namespace std;
class BankAccount {
private:
string owner;
double balance;
public:
BankAccount(string name, double initial) {
owner = name;
balance = initial;
}
void display() {
cout << owner << "'s Balance: " << balance << endl;
}
};
int main() {
BankAccount acc("Temwa", 1000);
acc.deposit(500);
acc.withdraw(200);
acc.display();
return 0;
}
#include <iostream>
#include <conio.h> // for getche() and getch()
using namespace std;
int main() {
char c;
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
float num = 123.45678;
return 0;
}
Conclusion
These programs demonstrate key aspects of C++ and OOP concepts, including
encapsulation, abstraction, inheritance, polymorphism, procedural vs. OOP design, real-
world modeling, and stream handling.