DETAILED NOTES: INHERITANCE, CLASSES & OBJECTS, CONSTRUCTORS, FILES, AND EXCEPTION
HANDLING IN C++
Inheritance
Definition: Inheritance is an object-oriented programming concept that allows one class (derived class)
to inherit the attributes and methods of another class (base class).
Types of Inheritance:
1. Single Inheritance: One derived class inherits from one base class.
2. Multiple Inheritance: One derived class inherits from more than one base class.
3. Multilevel Inheritance: A class is derived from a class which is also derived from another class.
4. Hierarchical Inheritance: Multiple derived classes inherit from a single base class.
5. Hybrid Inheritance: A combination of more than one type of inheritance.
Diagram: Types of Inheritance
Single: Multiple: Multilevel: Hierarchical:
A A B A A
| | | | |\
B C | B B C
\_/ |
C
Virtual Base Class:
• Used in hybrid inheritance to avoid ambiguity (diamond problem).
• Ensures only one copy of base class’s data members are inherited.
class A {
};
class B : virtual public A {
};
class C : virtual public A {
};
class D : public B, public C {
}; // Only one A is inherited
Abstract Base Class:
• A class that contains at least one pure virtual function.
• Cannot be instantiated.
class Shape {
public:
1
virtual void draw() = 0; // Pure virtual function
};
Classes and Objects
Definition:
• A class is a blueprint for creating objects.
• An object is an instance of a class.
Defining Data Members and Member Functions:
#include<iostream.h>
#include<conio.h>
class Student {
private:
int rollNo;
char name[20];
public:
void input() {
cout << "Enter Roll No and Name: ";
cin >> rollNo >> name;
}
void display() {
cout << "Roll No: " << rollNo << "\nName: " << name;
}
};
void main() {
clrscr();
Student s;
s.input();
s.display();
getch();
}
Functions in C++
• Inline functions: Defined inside the class.
• Static functions: Common for all objects.
• Friend functions: Not a class member but can access private members.
2
Constructors and Destructors in Derived Class
Constructor in Derived Class:
• Base class constructor executes first.
#include<iostream.h>
#include<conio.h>
class Base {
public:
Base() {
cout << "Base Constructor\n";
}
};
class Derived : public Base {
public:
Derived() {
cout << "Derived Constructor\n";
}
};
void main() {
clrscr();
Derived d;
getch();
}
Destructor in Derived Class:
~Derived() {
cout << "Derived Destructor\n";
}
~Base() {
cout << "Base Destructor\n";
}
• Destructors run in reverse order: Derived first, then Base.
File Handling in C++
Overview:
File handling in C++ allows programs to read from and write to files. It is crucial for persistent data
storage. Turbo C++ uses header file <fstream.h> for file operations.
3
File Modes:
• ios::in – Read mode
• ios::out – Write mode
• ios::app – Append mode
• ios::binary – Binary mode
• ios::ate – Go to the end of file
• ios::trunc – Truncate file if it exists
File Stream Classes:
• ifstream – For reading from a file
• ofstream – For writing to a file
• fstream – For both reading and writing
Opening a File:
ofstream fout("file.txt"); // Create and write
ifstream fin("file.txt"); // Read
fstream file("file.txt", ios::in | ios::out); // Read and write
Reading/Writing Strings and Characters:
char str[50];
ofstream fout("data.txt");
fout << "This is Turbo C++ file";
fout.close();
ifstream fin("data.txt");
fin.getline(str, 50);
cout << str;
fin.close();
File Pointers:
• seekg() – Set the get pointer (input position)
• seekp() – Set the put pointer (output position)
• tellg() – Get current get pointer position
• tellp() – Get current put pointer position
Example: Seek and Tell
ifstream fin("data.txt");
fin.seekg(0); // Move to beginning
int pos = fin.tellg();
cout << "Pointer at: " << pos;
4
File Updation Using Random Access:
#include<fstream.h>
#include<conio.h>
#include<iostream.h>
class Employee {
public:
int id;
char name[20];
void input() {
cout << "Enter ID and Name: ";
cin >> id >> name;
}
void display() {
cout << "ID: " << id << " Name: " << name << endl;
}
};
void main() {
clrscr();
Employee e;
fstream file("emp.dat", ios::in | ios::out | ios::binary);
cout << "--- Writing to File ---\n";
e.input();
file.write((char*)&e, sizeof(e));
file.seekg(0);
cout << "--- Reading from File ---\n";
file.read((char*)&e, sizeof(e));
e.display();
file.close();
getch();
}
Exception Handling
Various Exception Classes:
• Standard classes: exception , runtime_error (Turbo C++ doesn't fully support these, use
user-defined)
5
try and catch block:
#include<iostream.h>
#include<conio.h>
void main() {
clrscr();
int a = 10, b = 0;
try {
if (b == 0)
throw "Division by zero error";
cout << a / b;
} catch (const char* msg) {
cout << msg;
}
getch();
}
throw Keyword:
• Used for manually raising exceptions in Turbo C++
void divide(int a, int b) {
if (b == 0)
throw "Cannot divide by zero";
else
cout << "Result: " << a / b;
}