0% found this document useful (0 votes)
2 views5 pages

C++ Programming Assignment

The document outlines several C++ programming assignments that illustrate key OOP principles such as encapsulation, abstraction, inheritance, and polymorphism. It includes examples of procedural versus OOP styles, real-world modeling with a bank account system, and demonstrates console I/O and manipulators. The conclusion emphasizes the importance of these concepts in understanding C++ programming.
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)
2 views5 pages

C++ Programming Assignment

The document outlines several C++ programming assignments that illustrate key OOP principles such as encapsulation, abstraction, inheritance, and polymorphism. It includes examples of procedural versus OOP styles, real-world modeling with a bank account system, and demonstrates console I/O and manipulators. The conclusion emphasizes the importance of these concepts in understanding C++ programming.
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/ 5

C++ Programming Assignment

Program 1: Demonstration of OOP Principles


Objective: Demonstrate Encapsulation, Abstraction, Inheritance, and Polymorphism.

#include <iostream>
using namespace std;

// Encapsulation and Abstraction


class Person {
private:
string name;
int age;

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

Program 2: Evolution of C++ – Procedural vs. OOP


(a) Procedural Style:

#include <iostream>
using namespace std;

void calculateArea(float length, float width) {


float area = length * width;
cout << "Area: " << area << endl;
}

int main() {
calculateArea(5, 4);
return 0;
}

(b) OOP Style:


#include <iostream>
using namespace std;

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

Program 3: Real-world Modeling using C++


Example: Bank Account System

#include <iostream>
using namespace std;

class BankAccount {
private:
string owner;
double balance;

public:
BankAccount(string name, double initial) {
owner = name;
balance = initial;
}

void deposit(double amount) {


balance += amount;
}

void withdraw(double amount) {


if (amount <= balance)
balance -= amount;
else
cout << "Insufficient funds." << endl;
}

void display() {
cout << owner << "'s Balance: " << balance << endl;
}
};

int main() {
BankAccount acc("Temwa", 1000);
acc.deposit(500);
acc.withdraw(200);
acc.display();
return 0;
}

Program 4: Streams and Unformatted Console I/O

#include <iostream>
#include <conio.h> // for getche() and getch()
using namespace std;

int main() {
char c;

cout << "Press any key: ";


c = getch(); // does not echo
cout << "\nYou pressed (hidden): " << c << endl;

cout << "Press another key: ";


c = getche(); // echoes input
cout << "\nYou pressed (shown): " << c << endl;
return 0;
}

Program 5: Manipulators and Stream Classes

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

int main() {
float num = 123.45678;

cout << "Default: " << num << endl;


cout << "Fixed: " << fixed << setprecision(2) << num << endl;
cout << "Scientific: " << scientific << num << endl;

cout << setw(10) << right << "Right" << endl;


cout << setw(10) << left << "Left" << endl;

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.

You might also like