Object Orianted Programming-201-1
Object Orianted Programming-201-1
1. Polymorphism in C++
Polymorphism, a cornerstone of Object-Oriented Programming (OOP), is a
powerful concept that allows objects of different classes to be treated as
objects of a common type. It's like having a single interface that can
interact with diverse entities, making your code more flexible, reusable,
and maintainable. Polymorphism, derived from Greek words meaning
"many forms," refers to the ability of an object to take on multiple forms.
In simpler terms, it means that the same entity can exhibit different
behaviors in different contexts. There is two types of Polymorphism which
are:
1
Software Engineering/OOP
Year 2. Semester 2, Sheet:01 Dr. Thaer F. Ali
2
Software Engineering/OOP
Year 2. Semester 2, Sheet:01 Dr. Thaer F. Ali
#include <iostream>
using namespace std;
void display(int i) {
cout << "Integer: " << i << endl;
}
void display(double f) {
cout << "Double: " << f << endl;
}
void display(string s) {
cout << "String: " << s << endl;
}
int main() {
display(5);
display(3.14);
display("Hello");
return 0;
}
class Complex {
public:
Complex operator+(const Complex& other) const {
// ...
}
};
3
Software Engineering/OOP
Year 2. Semester 2, Sheet:01 Dr. Thaer F. Ali
overriding occurs when a derived class has a definition for one of the
member functions of the base class. That base function is overridden in
the derived class.
class Animal {
public:
virtual void makeSound() {
cout << "Generic animal sound" << endl;
}
};
#include <iostream>
using namespace std;
class Base {
public:
virtual void show() {
cout << "Base class" << endl;
}};
class Derived : public Base {
public:
void show() override {
cout << "Derived class" << endl;
}};
int main() {
4
Base* b;
Software Engineering/OOP
Year 2. Semester 2, Sheet:01 Dr. Thaer F. Ali
Derived d;
b = &d;
b->show();
return 0;}
2. Files in C++
File handling in C++ is a crucial aspect that allows programs to read from
and write to files. Imagine a program that calculates something, but when
you close it, all the data is lost. Files provide a way to store information
permanently, so your programs can read, write, and manipulate data
across multiple runs. Think of saving a game, storing user settings, or
processing large datasets – all rely on file handling. This enables data
persistence, making it possible to store information even after the
program has ended. C++ provides a comprehensive set of classes for file
operations as part of its Standard Library. The fstream library provides
tools for working with files, including reading, writing, and appending
data.
5
Software Engineering/OOP
Year 2. Semester 2, Sheet:01 Dr. Thaer F. Ali
We use the fstream library, which provides classes for file input and
output. Here are the key classes:
6
Software Engineering/OOP
Year 2. Semester 2, Sheet:01 Dr. Thaer F. Ali
#include <iostream>
#include <fstream>
int main() {
std::ofstream myFile("my_data.txt"); // Opens "my_data.txt" for writing
if (myFile.is_open()) {
// File opened successfully
myFile << "Hello, File!" << std::endl; // Writing to the file
myFile.close(); // Closing the file is crucial!
} else {
std::cerr << "Unable to open file" << std::endl;
return 1; // Indicate an error
}
return 0;
}
#include <iostream>
#include <fstream>
#include <string>
int main() {
7
Software Engineering/OOP
Year 2. Semester 2, Sheet:01 Dr. Thaer F. Ali
std::ifstream
myFile("my_data.txt"); if
(myFile.is_open()) {
std::string line;
// Reading integers and strings from the file using the >>
myFile.close();
} else {
return 0;
2.6 Writing to a File:
You can use the << operator to write various data types:
#include <iostream>
#include <fstream>
int main() {
std::ofstream outfile("example.txt");
if (outfile.is_open()) {
outfile << "This is a line." <<
std::endl; outfile << "This is another
line." << std::endl;
8
Software Engineering/OOP
Year 2. Semester 2, Sheet:01 Dr. Thaer F. Ali
outfile.close();
} else {
std::cout << "Unable to open file." << std::endl;
}
return 0;
}
#include <iostream>
#include <fstream>
#include <string>
int main() {
std::ifstream
infile("example.txt"); if (!
infile) {
error code
} else {
std::string line;
std::endl;
infile.close();
9
Software Engineering/OOP
Year 2. Semester 2, Sheet:01 Dr. Thaer F. Ali
Key Takeaways:
Include <fstream> for file operations.
Use ofstream, ifstream, or fstream objects.
Open files with appropriate modes.
Always check for errors when opening files.
Close files when finished.
Use getline() for reading text files line by line.
Use read() and write() for binary files.
#include <iostream>
int main()
{ try {
// Code that may throw an exception
throw 20;
} catch (int e) {
// Code to handle the exception
std::cerr << "An exception occurred. Exception Nr. " << e << std::endl;
}
return 0;
}
10
Software Engineering/OOP
Year 2. Semester 2, Sheet:01 Dr. Thaer F. Ali
#include <stdexcept>
Custom Exceptions: You can define your own exceptions by inheriting from the std::exception
class.
#include <iostream>
#include <exception>
11
Software Engineering/OOP
Year 2. Semester 2, Sheet:01 Dr. Thaer F. Ali
Detailed Breakdown
1. try Block:
o Code that might throw an exception is enclosed within the try
block.
o If an exception is thrown, the program immediately jumps to
the corresponding catch block, skipping the remaining code in
the try block.
2. throw Statement:
3. catch Block:
4. e.what():
12
Software Engineering/OOP
Year 2. Semester 2, Sheet:01 Dr. Thaer F. Ali
14
Software Engineering/OOP
Year 2. Semester 2, Sheet:01 Dr. Thaer F. Ali
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> numbers = {1, 3, 5, 4, 2};
return 0;
}
15