Object-Oriented Programming (OOPS) - Handwritten Notes
1. Variables, Data Types, Constants, Operators
### Definition:
Variables are named storage locations in memory. Data types define what type of data a variable can hold.
Constants have fixed values that do not change. Operators perform operations on variables.
### Example Program:
#include <iostream>
using namespace std;
int main() {
int a = 10; // Integer variable
const float pi = 3.14; // Constant
int sum = a + 5; // Arithmetic operation
cout << "Sum: " << sum;
return 0;
}
### Explanation:
1. 'int a' declares an integer variable.
2. 'const float pi' creates a constant floating-point variable.
3. 'sum = a + 5' performs an addition.
4. 'cout' displays the result.
2. Control Structures and Functions
### Definition:
Control structures manage the flow of execution (if-else, loops). Functions modularize code for reuse.
### Example Program:
#include <iostream>
using namespace std;
void greet() {
cout << "Hello, World!";
}
int main() {
greet(); // Function call
return 0;
}
### Explanation:
1. 'void greet()' defines a function.
2. 'cout' prints text.
3. 'greet()' is called inside main().
3. Arrays and Pointers
### Definition:
Arrays store multiple values of the same type. Pointers store memory addresses.
Object-Oriented Programming (OOPS) - Handwritten Notes
### Example Program:
#include <iostream>
using namespace std;
int main() {
int arr[3] = {1, 2, 3}; // Array declaration
int* ptr = arr; // Pointer stores array address
cout << "First element: " << *ptr;
return 0;
}
### Explanation:
1. 'arr[3]' creates an array.
2. 'ptr = arr' assigns array address to pointer.
3. '*ptr' accesses first element.
4. Classes and Objects
### Definition:
A class is a blueprint for objects. An object is an instance of a class.
### Example Program:
#include <iostream>
using namespace std;
class Car {
public:
string brand;
void show() { cout << "Car brand: " << brand; }
};
int main() {
Car myCar; // Object creation
myCar.brand = "Toyota"; // Assign value
myCar.show();
return 0;
}
### Explanation:
1. 'class Car' defines a class.
2. 'string brand' is a data member.
3. 'show()' is a method.
4. 'Car myCar' creates an object.
5. 'myCar.brand = "Toyota"' assigns value.
6. 'myCar.show()' calls method.
5. Inheritance
### Definition:
Inheritance allows a class to derive properties from another class.
### Example Program:
Object-Oriented Programming (OOPS) - Handwritten Notes
#include <iostream>
using namespace std;
class Vehicle {
public:
int wheels;
};
class Car : public Vehicle {
public:
string brand;
};
int main() {
Car myCar;
myCar.wheels = 4;
myCar.brand = "BMW";
cout << "Car brand: " << myCar.brand << ", Wheels: " << myCar.wheels;
return 0;
}
### Explanation:
1. 'class Vehicle' is a base class.
2. 'class Car : public Vehicle' derives from Vehicle.
3. 'myCar.wheels' accesses inherited property.
6. Polymorphism
### Definition:
Polymorphism allows methods to have different implementations.
### Example Program:
#include <iostream>
using namespace std;
class Animal {
public:
virtual void sound() { cout << "Animal sound"; }
};
class Dog : public Animal {
public:
void sound() override { cout << "Bark"; }
};
int main() {
Animal* a = new Dog();
a->sound();
return 0;
}
### Explanation:
1. 'virtual void sound()' enables polymorphism.
2. 'Dog::sound()' overrides base class function.
3. 'a->sound()' calls derived class method dynamically.
Object-Oriented Programming (OOPS) - Handwritten Notes
7. Constructor and Destructor
### Definition:
A constructor initializes objects. A destructor releases memory.
### Example Program:
#include <iostream>
using namespace std;
class Car {
public:
Car() { cout << "Car created"; }
~Car() { cout << "Car destroyed"; }
};
int main() {
Car myCar;
return 0;
}
### Explanation:
1. 'Car()' is a constructor that runs automatically.
2. '~Car()' is a destructor that runs when object is destroyed.
8. Operator Overloading
### Definition:
Allows redefining the behavior of operators for objects.
### Example Program:
#include <iostream>
using namespace std;
class Complex {
public:
int real, img;
Complex operator+(Complex c) {
Complex temp;
temp.real = real + c.real;
temp.img = img + c.img;
return temp;
}
};
int main() {
Complex c1 = {3, 2}, c2 = {1, 7};
Complex c3 = c1 + c2;
cout << "Sum: " << c3.real << " + " << c3.img << "i";
return 0;
}
### Explanation:
1. 'operator+' overloads '+'.
Object-Oriented Programming (OOPS) - Handwritten Notes
2. 'c1 + c2' invokes overloaded function.