C++ Full Notes with Important Programs
**C++ Notes from BTech Syllabus PDF**
**Course Title:** Object-Oriented Programming using C++
---
### Unit I: Introduction to Object-Oriented Programming
- Need for object-oriented programming.
- Characteristics of object-oriented languages.
- Class and objects, constructors, destructors.
- Objects as function arguments, returning objects.
**Example Program: Creating a simple class and object**
```cpp
#include <iostream>
using namespace std;
class Student {
public:
string name;
int age;
void display() {
cout << "Name: " << name << " Age: " << age << endl;
}
};
int main() {
Student s1;
s1.name = "Rahul";
s1.age = 20;
s1.display();
return 0;
}
```
---
### Unit II: Operator Overloading and Inheritance
- Overloading unary and binary operators.
- Data conversion.
- Derived and base class, public and private inheritance.
- Multiple inheritance examples.
**Example Program: Operator Overloading**
```cpp
#include <iostream>
using namespace std;
class Complex {
public:
int real, imag;
Complex(int r, int i) { real = r; imag = i; }
Complex operator + (Complex const &obj) {
return Complex(real + obj.real, imag + obj.imag);
}
void display() { cout << real << " + " << imag << "i" << endl; }
};
int main() {
Complex c1(3, 4), c2(2, 5);
Complex c3 = c1 + c2;
c3.display();
return 0;
}
```
---
### Unit III: Polymorphism
- Virtual functions, dynamic binding.
- Abstract classes and pure virtual functions.
- Friend functions, the "this" pointer.
**Example Program: Function Overloading**
```cpp
#include <iostream>
using namespace std;
class Math {
public:
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
};
int main() {
Math obj;
cout << obj.add(5, 10) << endl;
cout << obj.add(3.5, 2.5) << endl;
return 0;
}
```
---
### Unit IV: Streams and File Handling
- Streams, stream output and input.
- Stream manipulators.
- File handling: creating, reading, and updating sequential and
random files.
**Example Program: File Handling**
```cpp
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ofstream file("example.txt");
file << "Hello, this is a test file!";
file.close();
string text;
ifstream readFile("example.txt");
while (getline(readFile, text)) {
cout << text << endl;
}
readFile.close();
return 0;
}
```
---
### Unit V: Templates and Exception Handling
- Function templates, overloading function templates.
- Class templates.
- Exception handling overview, need for exceptions.
- Multiple exceptions, exception specifications.
**Example Program: Function Template**
```cpp
#include <iostream>
using namespace std;
template <typename T>
T square(T num) {
return num * num;
}
int main() {
cout << square(5) << endl;
cout << square(2.5) << endl;
return 0;
}
```
---
### Unit VI: Standard Template Library (STL)
- Introduction to STL: containers, iterators, algorithms.
- Sequence containers, associative containers, container adapters.
**Example Program: Using Vectors in STL**
```cpp
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> v = {10, 20, 30, 40};
v.push_back(50);
for (int i : v) {
cout << i << " ";
}
return 0;
}
```
---
### Textbooks:
1. E. Balagurusamy, *Object-Oriented Programming with C++*,
McGraw-Hill.
2. Robert Lafore, *Object-Oriented Programming in C++*, Sams
Publishing.
3. Dr. B. B. Meshram, *Object-Oriented Paradigms with C++*, SPD
Publication.
4. Rajesh R. Shukla, *Object-Oriented Programming in C++*, Wiley
India.
### Reference Books:
1. P. J. Deitel, H. M. Deitel, *C++ How to Program*, PHI.
2. John Hubbard, *Programming with C++*, Schaum's Outlines,
McGraw-Hill.
3. Nicolai M. Josuttis, *Object-Oriented Programming in C++*, Wiley.
**End of Notes**