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

OOP Programs

Uploaded by

aditaypatil07
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)
4 views6 pages

OOP Programs

Uploaded by

aditaypatil07
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

1.

Static Data Member and Static Member Function

#include <iostream>

using namespace std;

class Counter {

static int count;

public:

static void increment() { count++; }

static int getCount() { return count; }

};

int Counter::count = 0;

int main() {

Counter::increment();

Counter::increment();

cout << "Count: " << Counter::getCount() << endl;

return 0;

2. Abstract Class

#include <iostream>

using namespace std;

class Shape {

public:
virtual void area() = 0; // Pure virtual function

};

class Circle : public Shape {

double radius;

public:

Circle(double r) : radius(r) {}

void area() override {

cout << "Area of Circle: " << 3.14 * radius * radius << endl;

};

int main() {

Circle c(5.0);

c.area();

return 0;

3. File I/O Operations

#include <iostream>

#include <fstream>

using namespace std;

int main() {

// Write to a file

ofstream outFile("example.txt");

outFile << "Hello, File I/O!" << endl;


outFile.close();

// Read from a file

ifstream inFile("example.txt");

string line;

while (getline(inFile, line)) {

cout << line << endl;

inFile.close();

return 0;

4. Employee Details (File Read/Write)

#include <iostream>

#include <fstream>

using namespace std;

class Employee {

string name;

int id;

float salary;

public:

void getDetails() {

cout << "Enter Name, ID, and Salary: ";

cin >> name >> id >> salary;

}
void displayDetails() const {

cout << "Name: " << name << ", ID: " << id << ", Salary: " << salary << endl;

void writeToFile() {

ofstream outFile("employee.txt");

outFile << name << " " << id << " " << salary << endl;

outFile.close();

void readFromFile() {

ifstream inFile("employee.txt");

inFile >> name >> id >> salary;

inFile.close();

};

int main() {

Employee emp;

emp.getDetails();

emp.writeToFile();

emp.readFromFile();

emp.displayDetails();

return 0;

5. Dynamic Polymorphism (Function Overriding)

#include <iostream>

using namespace std;


class Base {

public:

virtual void display() {

cout << "Base class display" << endl;

};

class Derived : public Base {

public:

void display() override {

cout << "Derived class display" << endl;

};

int main() {

Base* b;

Derived d;

b = &d;

b->display();

return 0;

6. Dynamic Memory Allocation

#include <iostream>

using namespace std;


int main() {

int* ptr = new int(10);

cout << "Value: " << *ptr << endl;

delete ptr;

return 0;

7. Exception Handling

#include <iostream>

using namespace std;

int main() {

try {

int a, b;

cout << "Enter two numbers: ";

cin >> a >> b;

if (b == 0) throw "Division by zero!";

cout << "Result: " << a / b << endl;

catch (const char* msg) {

cout << "Error: " << msg << endl;

return 0;

You might also like