0% found this document useful (0 votes)
2 views10 pages

C++ Unit 1 Notes

C++ is a high-level, object-oriented programming language developed by Bjarne Stroustrup, supporting both procedural and object-oriented programming paradigms. Key features include encapsulation, inheritance, polymorphism, and a rich standard library, making it suitable for various applications. The language also adheres to ISO/IEC standards, with modern features introduced in recent versions.

Uploaded by

Jayesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views10 pages

C++ Unit 1 Notes

C++ is a high-level, object-oriented programming language developed by Bjarne Stroustrup, supporting both procedural and object-oriented programming paradigms. Key features include encapsulation, inheritance, polymorphism, and a rich standard library, making it suitable for various applications. The language also adheres to ISO/IEC standards, with modern features introduced in recent versions.

Uploaded by

Jayesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

1.

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.

Key Features of C++:

 Object-Oriented: Supports classes and objects, encapsulation, inheritance, and polymorphism.


 Low-level operations: Direct memory manipulation via pointers.
 Standard Library: Includes a rich set of libraries for data structures, algorithms, and I/O operations.
 Portability: C++ code can run on various platforms with minimal changes.

2. C++ Characteristics

Characteristics of C++:

 Object-Oriented: Supports OOP features like encapsulation, inheritance, and polymorphism.


 Memory Management: Manual memory management via pointers and dynamic memory allocation.
 Function Overloading: Functions can have the same name but different parameters.
 Operator Overloading: You can define custom behavior for operators.
 Multi-Paradigm: Supports procedural, object-oriented, and generic programming.
 Extensive Standard Library: Includes many built-in data structures, algorithms, and utilities (Standard
Template Library).

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++:

 Compile-Time Polymorphism (Static): Achieved through function overloading and operator


overloading.
 Run-Time Polymorphism (Dynamic): Achieved through inheritance and virtual functions.

Example of Compile-Time Polymorphism:

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;
}

Example of Run-Time Polymorphism:

class Animal {
public:
virtual void sound() {
cout << "Some animal sound" << endl;
}
};

class Dog : public Animal {


public:
void sound() override {
cout << "Bark" << 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.

 Private members: Accessible only within the class.


 Public members: Accessible from anywhere.

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.

 Single Inheritance: A class inherits from a single base class.


 Multiple Inheritance: A class inherits from multiple base classes.

Example of Inheritance:

class Animal {
public:
void eat() {
cout << "Eating food" << endl;
}
};

class Dog : public Animal {


public:
void bark() {
cout << "Barking" << 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.

8. Abstract Data Types (ADT)

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.

Common ADTs in C++:

 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.

 cin: For input from the user.


 cout: For output to the console.
 cerr: For error messages (unbuffered).
 clog: For logging messages (buffered).

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;
}

10. Standard Template Library (STL)

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).

 Containers: Store collections of objects (e.g., vector, list, map).


 Algorithms: Perform operations on data structures (e.g., sort, find).
 Iterators: Traverse containers.

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;
}

11. Standards Compliance

C++ is governed by the ISO/IEC C++ standard, with updates over the years. The major standards are:

 C++98: The first standardized version.


 C++03: A minor revision.
 C++11, C++14, C++17, C++20: Introduced modern features like lambda expressions, auto keyword,
smart pointers, and concurrency.

Most compilers today (like GCC, MSVC, Clang) are compliant with modern C++ standards (C++11 and later).

12. Functions and Variables. Declaration and Definition

 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

Type Description Example


Compile-Time Resolved at compile time using function overloading or operator overloading. Function Overloading
Run-Time Resolved at runtime using inheritance and virtual functions. Virtual Functions

Example of Runtime Polymorphism:

class Animal {
public:
virtual void sound() { cout << "Animal Sound" << endl; }
};

class Dog : public Animal {


public:
void sound() override { cout << "Bark" << endl; }
};

int main() {
Animal* animal = new Dog();
animal->sound(); // Calls Dog's sound method at runtime
return 0;
}

5. Encapsulation

Concept Description Example


Restricting direct access to class members and providing access Private data members and public
Data Hiding
through methods (getters and setters). methods for access.
Access Defines the visibility of class members: public, private, Class with private variables and
Specifiers protected. public methods.

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

Type Description Example


Single Inheritance A derived class inherits from a single base class. class Dog : public Animal {}
Multiple A derived class inherits from multiple base class Hybrid : public Animal, public Machine
Inheritance classes. {}

Example:

class Animal {
public:
void eat() { cout << "Eating food" << endl; }
};

class Dog : public Animal {


public:
void bark() { cout << "Barking" << 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.

8. Abstract Data Types (ADT)

ADT Description Example


Stack LIFO (Last In, First Out) structure. Push, Pop, Top
Queue FIFO (First In, First Out) structure. Enqueue, Dequeue, Front
List Collection of elements with no specific order. Insert, Remove, Search

9. I/O Services

I/O Type Description Example


cin Standard input stream for reading from the keyboard. cin >> var;
cout Standard output stream for printing to the console. cout << "Hello World";
cerr Standard error stream for printing error messages (unbuffered). cerr << "Error occurred";

10. Standard Template Library (STL)

Component Description Example


Containers Data structures like vector, list, map, set, etc. vector<int> v;
Algorithms Functions for sorting, searching, etc. sort(v.begin(), v.end());
Iterators Used to traverse containers. for (auto it = v.begin(); it != v.end(); ++it)

11. Standards Compliance

Standard Year Key Features


C++98 1998 First ISO standard for C++, introduced basic syntax, object-oriented features.
C++03 2003 Minor revisions and bug fixes, no major changes.
C++11 2011 Introduction of auto, lambdas, smart pointers, and concurrency features.
C++14 2014 Bug fixes and performance improvements.
C++17 2017 New features like filesystem library, std::optional, and structured bindings.
C++20 2020 Major updates like concepts, ranges, and coroutines.

12. Functions and Variables: Declaration and Definition


Concept Description Example
Function Declaration Declaring a function signature without defining it. int add(int, int);
Function Definition Providing the implementation for the declared function. int add(int a, int b) { return a + b; }
Variable Declaration Declaring a variable without initializing it. int x;
Variable Definition Declaring and initializing a variable. int x = 10;

You might also like