Professional Documents
Culture Documents
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.
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.
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.
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.
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 .
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
}
Definition : Type conversion, or casting, is the process of converting a variable from one data type to another.
Example :
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 :
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 :
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
}
Definition : Looping statements, such as for, while, and do-while, allow executing a block of code repeatedly.
Example :
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 :
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];
};
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 :
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;
}
};
Definition : Pointers can be used to create dynamic objects and manipulate class instances.
Example :
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
}
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
}
};
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;
};
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);
};
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;
}
};
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.
Types :
Single Inheritance
Multiple Inheritance
Multilevel Inheritance
Hierarchical Inheritance
class Base {
public:
Base() {
cout << "Base class constructor." << endl;
}
~Base() {
cout << "Base class destructor." << 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.
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;
}
Definition : Operator overloading allows defining how operators behave for user-defined types.
Example :
class Complex {
public:
int real;
int imag;
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;
}
};
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
};
➟ 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;
Opening Files :
Example :
ifstream inputFile;
inputFile.open("data.txt");
Closing Files :
Example :
inputFile.close();
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) :
public:
Pair(T f, T s) : first(f), second(s) {}
T getFirst() const {
return first;
}
T getSecond() const {
return second;
}
};
try {
// Code that might throw an exception
throw runtime_error("An error occurred");
} catch (const exception& e) {
cout << "Exception caught: " << e.what() << endl;
}