C++ Unit 1 Notes
C++ Unit 1 Notes
C++ Overview
C++ is a high-level, object-oriented programming language that is an extension of the C language. It was designed
and developed by Bjarne Stroustrup in the early 1980s. C++ supports both procedural programming (like C) and
object-oriented programming (OOP), making it suitable for a wide range of applications, from system software to
game development.
2. C++ Characteristics
Characteristics of C++:
Example:
#include <iostream>
using namespace std;
class Person {
private:
string name;
int age;
public:
Person(string n, int a) : name(n), age(a) {}
void display() {
cout << "Name: " << name << ", Age: " << age << endl;
}
};
int main() {
Person p("John", 25);
p.display();
return 0;
}
3. Object-Oriented Terminology
Class: A blueprint for creating objects. It defines the properties (data members) and behaviors (functions or
methods) of an object.
Object: An instance of a class. It has the attributes and methods defined by the class.
Method: A function defined inside a class that operates on its data members.
Constructor: Special function used to initialize objects.
Destructor: A special function used to clean up when an object is destroyed.
Access Specifiers: Defines the visibility of class members (public, private, protected).
Example:
class Car {
private:
string model;
public:
Car(string m) : model(m) {}
void display() {
cout << "Car model: " << model << endl;
}
};
4. Polymorphism
Polymorphism means "many forms." It allows functions or methods to behave differently based on the object they
are acting upon. There are two types of polymorphism in C++:
class Printer {
public:
void print(int i) {
cout << "Printing integer: " << i << endl;
}
void print(string s) {
cout << "Printing string: " << s << endl;
}
};
int main() {
Printer p;
p.print(5); // Prints integer
p.print("Hello"); // Prints string
return 0;
}
class Animal {
public:
virtual void sound() {
cout << "Some animal sound" << endl;
}
};
int main() {
Animal* animal = new Dog();
animal->sound(); // Calls Dog's sound method due to dynamic polymorphism
return 0;
}
5. Encapsulation
Encapsulation is the bundling of data (variables) and methods (functions) that operate on the data within one class.
It restricts direct access to some of an object's components and only allows access through methods.
Example:
class Account {
private:
double balance;
public:
void deposit(double amount) {
if (amount > 0) balance += amount;
}
double getBalance() {
return balance;
}
};
int main() {
Account acc;
acc.deposit(1000);
cout << "Balance: " << acc.getBalance() << endl;
return 0;
}
6. Inheritance
Inheritance allows a class to inherit attributes and methods from another class. It establishes a relationship between
base and derived classes.
Example of Inheritance:
class Animal {
public:
void eat() {
cout << "Eating food" << endl;
}
};
int main() {
Dog d;
d.eat(); // Inherited method
d.bark(); // Dog-specific method
return 0;
}
7. Object-Oriented Paradigm
The Object-Oriented Paradigm (OOP) emphasizes the use of objects which contain both data and methods. The
key principles of OOP are:
Encapsulation: Hiding the internal state of an object and providing controlled access via methods.
Abstraction: Hiding complex implementation details and exposing only necessary parts.
Inheritance: Reusing code from a base class in derived classes.
Polymorphism: The ability to treat objects of different types in a unified way.
An Abstract Data Type (ADT) is a model for a data structure that defines the behavior of the data but not its
implementation. ADTs allow for separation of what operations can be performed on data and how the data is
represented.
Stack (LIFO)
Queue (FIFO)
List
Tree
Example (Stack):
#include <iostream>
#include <stack>
using namespace std;
int main() {
stack<int> s;
s.push(10);
s.push(20);
s.push(30);
cout << "Top element: " << s.top() << endl; // Shows the top element (30)
s.pop(); // Removes the top element
cout << "Top element after pop: " << s.top() << endl; // Shows 20
return 0;
}
9. I/O Services
C++ provides input and output services through the iostream library. You can use cin, cout, cerr, and clog for
various I/O operations.
Example:
#include <iostream>
using namespace std;
int main() {
int age;
cout << "Enter your age: ";
cin >> age;
cout << "You are " << age << " years old." << endl;
return 0;
}
The Standard Template Library (STL) is a collection of template classes and functions that implement common
data structures (like vectors, lists, sets, and maps) and algorithms (like sort and find).
Example:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> v = {1, 2, 3, 4, 5};
sort(v.begin(), v.end(), greater<int>()); // Sort in descending order
for (int n : v) cout << n << " ";
return 0;
}
C++ is governed by the ISO/IEC C++ standard, with updates over the years. The major standards are:
Most compilers today (like GCC, MSVC, Clang) are compliant with modern C++ standards (C++11 and later).
Function Declaration: Declares the function signature (name, return type, parameters).
int add(int, int); // Function declaration
Function Definition: Provides the implementation.
int add(int a, int b) { // Function definition
return a + b;
}
Variable Declaration: Specifies the type and name of the variable.
int x; // Declaration
Variable Definition: Allocates memory and assigns a value.
int x = 10; // Declaration and definition
Certainly! Below is a more refined version of the explanations without the diagrams but maintaining the tables for
clarity:
1. C++ Overview
Feature Description
Type General-purpose, high-level programming language
Origin Developed by Bjarne Stroustrup in 1983 as an extension of C.
Multi-Paradigm Supports procedural, object-oriented, and generic programming
Feature Description
Memory Management Allows manual memory management using pointers and dynamic allocation
Use Cases System software, game development, embedded systems, high-performance applications
2. C++ Characteristics
Characteristic Description
Object-Oriented Supports classes, inheritance, polymorphism, and encapsulation.
Memory
Manual control over memory using pointers and dynamic allocation (new/delete).
Management
High performance with low-level memory manipulation, suitable for resource-intensive
Performance
applications.
Standard Library Includes libraries like STL, iostream, algorithms, and containers.
Portability Platform-independent (compilers for different systems).
3. Object-Oriented Terminology
Term Description
A blueprint for creating objects that defines properties (data members) and behaviors
Class
(methods/functions).
Object An instance of a class. It contains specific values for the class properties.
Bundling the data and methods that operate on that data into a single unit (class), while restricting
Encapsulation
access to some data.
Constructor Special method used to initialize an object when it is created.
Destructor Method called when an object goes out of scope or is deleted to clean up memory.
Inheritance Mechanism where a new class derives properties and behaviors from an existing class.
4. Polymorphism
class Animal {
public:
virtual void sound() { cout << "Animal Sound" << endl; }
};
int main() {
Animal* animal = new Dog();
animal->sound(); // Calls Dog's sound method at runtime
return 0;
}
5. Encapsulation
Example:
class Account {
private:
double balance;
public:
void setBalance(double bal) { balance = bal; }
double getBalance() { return balance; }
};
int main() {
Account acc;
acc.setBalance(1000);
cout << acc.getBalance() << endl; // 1000
}
6. Inheritance
Example:
class Animal {
public:
void eat() { cout << "Eating food" << endl; }
};
int main() {
Dog d;
d.eat();
d.bark();
return 0;
}
7. Object-Oriented Paradigm
Concept Description
Encapsulation Grouping data and methods together while hiding implementation details.
Abstraction Hiding the complex implementation and exposing only the essential details.
Inheritance Reusing code from a base class in a derived class.
Polymorphism The ability for objects of different types to respond to the same method call in different ways.
9. I/O Services