Talha Oop Assignment

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Pointers

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:

ClassName *ptrObjectName = new ClassName;

Example:

#include

<iostream>class

MyClass { public:

void display() {

std::cout << "Hello from MyClass!" << std::endl;

};

int main() {

MyClass *ptrObj = new MyClass; // Creating an object using a

pointerptrObj->display(); // Accessing member function using the

pointer delete ptrObj; // Deallocating memory to avoid memory

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:

class DerivedClass : accessSpecifier BaseClass

Example:

#include <iostream>

class Shape {

public:

void setWidth(int w) {

width = w;

void setHeight(int h) {

height = h;

protected:

int width;

int height;

};

class Rectangle : public Shape {

public:

int getArea() {

return width * height;

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

virtual void print() {

};

Example:

#include <iostream>

using namespace std;

class Base {

public:
virtual void print() {

cout << "Base Function" << endl;

};

class Derived : public Base {

public:

void print() {

cout << "Derived Function" << endl;

};

int main() {

Derived derived1;

Base* base1 = &derived1;

base1->print();

return 0;

Pure Virtual Function

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:

virtual void pureVirtualFunction() = 0;

};

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:

template <typename T>

T functionName(T parameter1, T parameter2) {

template <typename T>

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:

template <typename T>

ReturnType functionName(T parameter1, T parameter2) {

Example:

#include <iostream>

template <typename T>

T findMax(T a, T b) {

return (a > b) ? a : b;

int main() {

int maxInt = findMax(7, 12);

std::cout << "Maximum of 7 and 12: " << maxInt << std::endl;

double maxDouble = findMax(3.5, 2.7);

std::cout << "Maximum of 3.5 and 2.7: " << maxDouble << std::endl;

std::string maxString = findMax("apple", "orange");

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:

template <typename T>

class ClassName {

public:

};

Example:

#include <iostream>

template <typename T>

class Container {

private:

T value;

public:

Container(T val) : value(val) {}

T getValue() {

return value;

};

int main() {

Container<int> intContainer(42);

std::cout << "Value in intContainer: " << intContainer.getValue() << std::endl;

Container<double> doubleContainer(3.14);

std::cout << "Value in doubleContainer: " << doubleContainer.getValue() << std::endl;

Container<std::string> strContainer("Hello, Templates!");

std::cout << "Value in strContainer: " << strContainer.getValue() << std::endl;


return 0;

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:

static void writeFile(const std::string& filename, const std::string& content) {

std::ofstream outFile(filename);

if (outFile.is_open()) {

outFile << content;

std::cout << "File '" << filename << "' written successfully." << std::endl;

inFile.close();

} else {

std::cerr << "Error opening file '" << filename << "' for reading." << std::endl;

};

int main() {

FileHandler::writeFile("example.txt", "Hello, File Handling in C++!");


FileHandler::readFile("example.txt");

return 0;

File Access Methods

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:

static void readFile(const std::string& filename) {

std::ifstream inFile(filename);

if (inFile.is_open()) {

std::cout << "Contents of file '" << filename << "':" << std::endl;

std::string line;

while (std::getline(inFile, line)) {

std::cout << line << std::endl;

inFile.close();

} else {
std::cerr << "Error opening file '" << filename << "' for reading." << std::endl;

};

int main()

FileHandler::writeFile("example.txt", "Hello, File Handling in C++!");

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:

static void writeToConsole(const std::string& data) {

std::cout << "Console Output: " << data << std::endl;

static void writeToFile(const std::string& filename, const std::string& data) {


std::ofstream outFile(filename)

static void readFromFile(const std::string& filename) {

std::ifstream inFile(filename);

if (inFile.is_open()) {

std::cout << "Contents of file '" << filename << "':" << std::endl;

std::string line;

while (std::getline(inFile, line)) {

std::cout << line << std::endl;

inFile.close();

} else {

std::cerr << "Error opening file '" << filename << "' for reading." << std::end;

};

int main() {

StreamHandler::writeToConsole("Hello, Stream Handling in C++!");

StreamHandler::writeToFile("example.txt", "Hello, Stream Handling in C++!");

StreamHandler::readFromFile("example.txt");

return 0;

Opening & Closing File

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:

FileHandler(const std::string& filename) {

outFile.open(filename);

if (!outFile.is_open()) {

std::cerr << "Error opening file '" << filename << "' for writing." << std::endl;

~FileHandler() {

if (outFile.is_open()) {

outFile.close();

std::cout << "File closed successfully." << std::endl;

}
void writeToFile(const std::string& content) {

if (outFile.is_open()) {

outFile << content;

std::cout << "Content written to file." << std::endl;

} else {

std::cerr << "File is not open for writing." << std::endl;

};

int main() {

FileHandler file("example.txt");

file.writeToFile("Hello, Opening and Closing Files in C++!");

return 0;

You might also like