Professional Documents
Culture Documents
Talha Oop Assignment
Talha Oop Assignment
Talha Oop Assignment
Concept:
In OOP, pointers are used to manipulate objects dynamically. A pointer to an object allows
you to access and modify the object's properties and behaviors. It is particularly useful when
dealingwith dynamic memory allocation.
Syntax:
Example:
#include
<iostream>class
MyClass { public:
void display() {
};
int main() {
leaks
return 0;
Inheritance
Concept:
Inheritance in OOP allows a class to inherit properties and behaviors from another class,
fostering code reuse and the creation of a hierarchy of classes. The derived class can extend or
override the members of the base class.
Syntax:
Example:
#include <iostream>
class Shape {
public:
void setWidth(int w) {
width = w;
void setHeight(int h) {
height = h;
protected:
int width;
int height;
};
public:
int getArea() {
};
int main() {
Rectangle rect;
rect.setWidth(5);
rect.setHeight(10);
std::cout << "Area of the rectangle: " << rect.getArea() << std::endl;
return 0;
Virtual Function
Concept:
A virtual function is a function declared within a base class and marked with the virtual
keyword. It is intended to be overridden by derived classes. When a derived class provides a
specific implementation for a virtual function, it allows the program to invoke the most
appropriate version of the function based on the actual type of the object.
Syntax:
class Base {
public:
};
Example:
#include <iostream>
class Base {
public:
virtual void print() {
};
public:
void print() {
};
int main() {
Derived derived1;
base1->print();
return 0;
Concept:
A pure virtual function in C++ is a virtual function that is declared in a base class but has no
implementation. It is intended to be overridden by derived classes, and a class containing at
least one pure virtual function is known as an abstract class. Abstract classes cannot be
instantiated; they serve as a blueprint for derived classes to implement the pure virtual
functions.
Syntax:
class AbstractClass {
public:
};
Templates
Concept:
Templates in C++ allow you to create generic classes and functions that can work with different
data types. This promotes code reusability and flexibility by enabling the definition of
algorithms and data structures that are independent of the data type they operate on.
Syntax:
class ClassName {
public:
};
Function Template
Concept:
Function templates in C++ allow you to write a single function that can work with different
data types. The template mechanism enables generic programming, allowing you to create
flexible and reusable functions that operate on various data types without the need to write
multiple versions of the same function.
Syntax:
Example:
#include <iostream>
T findMax(T a, T b) {
return (a > b) ? a : b;
int main() {
std::cout << "Maximum of 7 and 12: " << maxInt << std::endl;
std::cout << "Maximum of 3.5 and 2.7: " << maxDouble << std::endl;
std::cout << "Maximum of 'apple' and 'orange': " << maxString << std::endl;
return 0;
Class Templates
Concept:
Class templates in C++ allow you to create generic classes that can work with different data
types. Similar to function templates, class templates provide a way to define classes with
members or methods that operate on generic types, enabling flexibility and code reusability.
Syntax:
class ClassName {
public:
};
Example:
#include <iostream>
class Container {
private:
T value;
public:
T getValue() {
return value;
};
int main() {
Container<int> intContainer(42);
Container<double> doubleContainer(3.14);
Files
Concept:
In C++, file handling involves working with files on the system, which includes reading from
and writing to files. Object-oriented programming in C++ allows you to encapsulate file
operations within classes, providing a more organized and modular approach to file handling.
Example:
#include <iostream>
#include <fstream>
class FileHandler {
public:
std::ofstream outFile(filename);
if (outFile.is_open()) {
std::cout << "File '" << filename << "' written successfully." << std::endl;
inFile.close();
} else {
std::cerr << "Error opening file '" << filename << "' for reading." << std::endl;
};
int main() {
return 0;
Concept:
File access methods in C++ involve reading from and writing to files using classes like
ifstream and ofstream from the <fstream> header. Object-oriented programming in C++
allows encapsulating file access operations within classes, providing a modular and organized
approach.
Example:
#include <iostream>
#include <fstream>
class FileHandler {
public:
std::ifstream inFile(filename);
if (inFile.is_open()) {
std::cout << "Contents of file '" << filename << "':" << std::endl;
std::string line;
inFile.close();
} else {
std::cerr << "Error opening file '" << filename << "' for reading." << std::endl;
};
int main()
FileHandler::readFile("example.txt");
return 0;
Stream
Concept:
In C++, streams are used to perform input and output operations on devices like the console,
files, etc. They provide a convenient and flexible way to handle data. Object-oriented
programming in C++ allows stream operations to be encapsulated within classes, providing a
modular and organized approach.
Example:
#include <iostream>
#include <fstream>
class StreamHandler {
public:
std::ifstream inFile(filename);
if (inFile.is_open()) {
std::cout << "Contents of file '" << filename << "':" << std::endl;
std::string line;
inFile.close();
} else {
std::cerr << "Error opening file '" << filename << "' for reading." << std::end;
};
int main() {
StreamHandler::readFromFile("example.txt");
return 0;
Concept:
In C++, opening and closing files involves utilizing classes such as ofstream and ifstream
from the <fstream> header. Object-oriented programming (OOP) in C++ allows encapsulating
file operations within classes, providing a more organized and modular approach. Opening a
file involves creating an instance of ofstream or ifstream, while closing a file is done
automatically when the file stream object goes out of scope.
Example:
#include <iostream>
#include <fstream>
class FileHandler {
private:
std::ofstream outFile;
public:
outFile.open(filename);
if (!outFile.is_open()) {
std::cerr << "Error opening file '" << filename << "' for writing." << std::endl;
~FileHandler() {
if (outFile.is_open()) {
outFile.close();
}
void writeToFile(const std::string& content) {
if (outFile.is_open()) {
} else {
};
int main() {
FileHandler file("example.txt");
return 0;