You are on page 1of 8

C++ LANGUAGE

➟ Introduction to Object-Oriented Concepts


Topic : Evolution of OOP

Definition : Object-Oriented Programming (OOP) is a programming paradigm that has evolved over the years to address the complexities of software
development. It is characterized by organizing code into objects, which represent real-world entities and their interactions.
Example : In early programming, procedural languages like C focused on functions and data separately. OOP emerged as a response to the need for more
structured and modular code. For instance, consider a banking system. In procedural code, you might have separate functions for deposit, withdraw, and
balance check. In OOP, these functions could be grouped into an "Account" object.

Topic : OOP Paradigm, Advantages of OOP

Definition : The OOP paradigm is based on the concept of objects, which encapsulate both data and methods that operate on that data. Advantages of
OOP include code reusability, modularity, and easier maintenance.
Example : In OOP, you can create a class "Car" that encapsulates attributes (e.g., make, model) and methods (e.g., start, stop). By instantiating multiple
car objects, you can model a car rental system more efficiently.

Topic : Comparison between Functional Programming and OOP Approach

Definition : Functional programming focuses on functions and immutability, while OOP emphasizes objects and encapsulation. The comparison depends
on the problem domain and the development team's preferences.
Example : In functional programming, a list of numbers might be processed using a map function to square each number. In OOP, you'd create a
"NumberList" object with methods to perform operations on the list.

Topic : Characteristics of Object-Oriented Languages

1. Objects : Objects are instances of classes and represent real-world entities. They encapsulate data and methods.
2. Classes : Classes are blueprints for creating objects. They define the structure and behavior of objects.
3. Inheritance : Inheritance allows a class (subclass/derived) to inherit properties and methods from another class (superclass/base).
4. Reusability : OOP promotes code reusability through the creation of classes and objects, reducing redundancy.
5. User-Defined Data Types : OOP lets you define custom data types (classes) to model real-world concepts more accurately.
6. Polymorphism : Polymorphism allows objects of different classes to be treated as objects of a common superclass. It includes method overloading and
method overriding.

Topic : Polymorphism - Overloading

Definition : Method overloading is a form of polymorphism where multiple methods in a class have the same name but different parameter lists. The correct
method is called based on the number or types of arguments provided.
Syntax :

class Calculator {
public:
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
};

Example : In the above code, the add method is overloaded to work with both integers and doubles. The appropriate version is called based on the
argument types:

Calculator calc;
int result1 = calc.add(5, 3); // Calls int version
double result2 = calc.add(2.5, 3.5); // Calls double version

➟ Introduction to C++
Topic : C++ Tokens

Definition : C++ programs are composed of various building blocks known as tokens. Tokens can be keywords, identifiers, literals, operators, and special
symbols.
Example : In the statement int x = 5; , the tokens are int , x , = , and 5 .

Topic : Data Types


Definition : Data types in C++ specify the type of data that a variable can hold. Common data types include int, float, double, char, etc.
Syntax :

int age = 25;


float height = 5.8;
char grade = 'A';

Topic : Control Flow Operators

Definition : Control flow operators in C++ are used to manage the flow of program execution. They include if, else, switch, break, and continue.
Example :

if (condition) {
// code to be executed if the condition is true
} else {
// code to be executed if the condition is false
}

Topic : Type Conversion

Definition : Type conversion, or casting, is the process of converting a variable from one data type to another.
Example :

double price = 45.99;


int roundedPrice = static_cast<int>(price);

Topic : Variable Declaration

Definition : Variables are declared to store data in a program. Declaration includes the data type and the name of the variable.
Syntax :

int count;
float temperature;

Topic : Arrays

Definition : Arrays are collections of similar data types stored in contiguous memory locations. They are useful for storing and manipulating sets of data.
Syntax :

int numbers[5] = {1, 2, 3, 4, 5};

Topic : Statements and Expressions

Definition : Statements are complete lines of code that perform an action, while expressions are combinations of variables and operators that result in a
value.
Example :

int sum = a + b; // Expression


cout << "The sum is: " << sum; // Statement

Topic : Conditional Statements

Definition : Conditional statements, like if and switch, allow the program to make decisions based on specified conditions.
Example :

if (x > 0) {
// code to be executed if x is positive
}

Topic : Looping Statements

Definition : Looping statements, such as for, while, and do-while, allow executing a block of code repeatedly.
Example :

for (int i = 0; i < 5; ++i) {


// code to be executed 5 times
}

Topic : Functions

Definition : Functions in C++ are blocks of code that perform a specific task. They are designed for code reusability and modularity.
Syntax :
int add(int a, int b) {
return a + b;
}

Topic : Pointers

Definition : Pointers are variables that store memory addresses. They are powerful tools for dynamic memory allocation and manipulation.
Syntax :

int number = 10;


int *ptr = &number; // Pointer declaration

Topic : Structures

Definition : Structures allow bundling different data types under a single name. They are useful for organizing related data.
Syntax :

struct Student {
int rollNumber;
char name[50];
};

➟ Classes and Objects


Topic : Classes

Definition : Classes in C++ are user-defined data types that encapsulate data and the functions that operate on that data.
Example :

class Circle {
private:
double radius;

public:
void setRadius(double r) {
radius = r;
}

double getArea() {
return 3.14 * radius * radius;
}
};

Topic : Objects

Definition : Objects are instances of classes. They represent real-world entities and are created from a class blueprint.
Example :

Circle myCircle; // Creating an object of the Circle class


myCircle.setRadius(5.0);
double area = myCircle.getArea();

Topic : Defining Member Functions

Definition : Member functions are functions defined within a class. They operate on the class's data members.
Example :

class Rectangle {
private:
int length;
int width;

public:
void setDimensions(int l, int w) {
length = l;
width = w;
}

int calculateArea() {
return length * width;
}
};

Topic : Arrays of Class Objects


Definition : Arrays can be created using class types, allowing the creation of multiple objects of the same class.
Example :

Rectangle rectangles[3]; // Array of Rectangle objects


rectangles[0].setDimensions(3, 4);
int area = rectangles[0].calculateArea();

Topic : Pointers and Classes

Definition : Pointers can be used to create dynamic objects and manipulate class instances.
Example :

Rectangle* ptrRectangle = new Rectangle();


ptrRectangle->setDimensions(5, 6);
int area = ptrRectangle->calculateArea();
delete ptrRectangle; // Don't forget to free the memory

Topic : Passing Objects

Definition : Objects can be passed to functions by value or by reference.


Example :

void modifyRectangle(Rectangle& rect) {


rect.setDimensions(8, 8);
}

Rectangle myRectangle;
modifyRectangle(myRectangle);

Topic : Constructors

Definition : Constructors are special member functions that are called when an object is created. They initialize the object.
Types of Constructors :
Default Constructor
Parameterized Constructor
Example :

class Employee {
public:
Employee() {
// Default Constructor
}

Employee(int empID, string empName) {


// Parameterized Constructor
}
};

Topic : Destructors

Definition : Destructors are special member functions called when an object goes out of scope or is explicitly deleted. They clean up resources.
Example :

class FileHandler {
public:
~FileHandler() {
// Code to close the file
}
};

Topic : this Pointer

Definition : The this pointer is a pointer that points to the current object. It is used to refer to the object's members within its own class.
Example :

class Example {
private:
int value;

public:
void setValue(int value) {
this->value = value;
}
};
Topic : Access Specifiers

Definition : Access specifiers (public, private, protected) control the visibility of class members.
Example :

class MyClass {
public:
int publicVar;

private:
int privateVar;
};

Topic : Friend Functions

Definition : Friend functions are functions that are not members of a class but have access to its private members.
Example :

class MyClass {
private:
int privateVar;

public:
friend void friendFunction(MyClass obj);
};

Topic : Inline Functions

Definition : Inline functions are functions defined with the inline keyword. They are expanded in place, reducing function call overhead.
Example :

class MathOperations {
public:
inline int square(int x) {
return x * x;
}
};

➟ Inheritance
Topic : Introduction

Definition : Inheritance is a fundamental concept in object-oriented programming (OOP) that allows a new class (derived or child class) to inherit attributes
and behaviors from an existing class (base or parent class).
Example :

class Animal {
public:
void eat() {
cout << "Animal is eating." << endl;
}
};

class Dog : public Animal {


public:
void bark() {
cout << "Dog is barking." << endl;
}
};

Topic : Importance of Inheritance

Importance : Inheritance promotes code reuse, facilitates the creation of a hierarchy of classes, and supports polymorphism, allowing objects of derived
classes to be treated as objects of their base class.

Topic : Types of Inheritance

Types :
Single Inheritance
Multiple Inheritance
Multilevel Inheritance
Hierarchical Inheritance

Topic : Constructors and Destructors in Derived Classes


Constructors : Derived class constructors can call the base class constructor using an initialization list.
Destructors : Derived class destructors automatically call the base class destructor.
Example :

class Base {
public:
Base() {
cout << "Base class constructor." << endl;
}

~Base() {
cout << "Base class destructor." << endl;
}
};

class Derived : public Base {


public:
Derived() : Base() {
cout << "Derived class constructor." << endl;
}

~Derived() {
cout << "Derived class destructor." << endl;
}
};

Topic : Polymorphism

Definition : Polymorphism allows objects of different classes to be treated as objects of a common base class.

Topic : Function Overloading

Definition : Function overloading in polymorphism involves defining multiple functions in the same scope but with different parameters.
Example :

class MathOperations {
public:
int add(int a, int b) {
return a + b;
}

double add(double a, double b) {


return a + b;
}
};

Topic : Operator Overloading

Definition : Operator overloading allows defining how operators behave for user-defined types.
Example :

class Complex {
public:
int real;
int imag;

Complex operator+(const Complex& other) {


Complex result;
result.real = real + other.real;
result.imag = imag + other.imag;
return result;
}
};

Topic : Virtual Functions

Definition : Virtual functions allow dynamic binding and late binding. They are declared in the base class and overridden in derived classes.
Example :

class Shape {
public:
virtual void draw() {
cout << "Drawing a shape." << endl;
}
};

class Circle : public Shape {


public:
void draw() override {
cout << "Drawing a circle." << endl;
}
};

Topic : Pure Virtual Functions

Definition : Pure virtual functions have no implementation in the base class and must be overridden by derived classes.
Example :

class AbstractShape {
public:
virtual void draw() = 0; // Pure virtual function
};

class ConcreteCircle : public AbstractShape {


public:
void draw() override {
cout << "Drawing a circle." << endl;
}
};

➟ File Management
Topic : Handling Data Files (Sequential and Random Access)

Sequential Access : Reading or writing data in a sequential manner from the beginning to the end of a file.
Example (Reading) :

ifstream inputFile("data.txt");
int value;
while (inputFile >> value) {
// Process each value
}

Example (Writing) :

ofstream outputFile("output.txt");
outputFile << "Hello, World!" << endl;

Random Access : Directly accessing data at any position in a file.


Example :

fstream dataFile in | ios::out | ios::binary ;


int value;
dataFile.seekg(2 * sizeof(int)); // Move to the third integer
dataFile.read(reinterpret_cast<char*>(&value), sizeof(int));

Topic : Opening and Closing of Files

Opening Files :
Example :

ifstream inputFile;
inputFile.open("data.txt");

Closing Files :
Example :

inputFile.close();

Topic : Stream State Member Functions

Stream state member functions check the state of the stream (e.g., good(), fail(), eof()).
Example :

ifstream inputFile("data.txt");
if (inputFile.good()) {
// File is open and ready for reading
}
Topic : Operations on Files

File operations include reading, writing, and manipulating data within files using various functions.
Example (Copying File Content) :

ifstream sourceFile("source.txt");
ofstream destinationFile("destination.txt");

char ch;
while (sourceFile.get(ch)) {
destinationFile.put(ch);
}

Topic : Templates

Templates allow the creation of generic functions and classes that work with any data type.
Example (Function Template) :

template <typename T>


T add(T a, T b) {
return a + b;
}

Example (Class Template) :

template <typename T>


class Pair {
private:
T first;
T second;

public:
Pair(T f, T s) : first(f), second(s) {}

T getFirst() const {
return first;
}

T getSecond() const {
return second;
}
};

Topic : Exception Handling

Exception handling helps manage errors and unexpected situations in a program.


Example :

try {
// Code that might throw an exception
throw runtime_error("An error occurred");
} catch (const exception& e) {
cout << "Exception caught: " << e.what() << endl;
}

You might also like