π C++ Basics β A Beginner Friendly Guide
Welcome student! Letβs understand C++ step-by-step in a simple and engaging way.
π₯ Difference Between C and C++
- C β Procedural Programming (focus on functions).
- C++ β Object-Oriented Programming (focus on objects & classes).
- C does not support classes; C++ supports classes, objects, inheritance, polymorphism.
- C uses stdio.h; C++ uses iostream.
- C++ supports function overloading, operator overloading, templates, exceptions.
π Basic C++ Library: <iostream>
To take input or print output, C++ uses the library:
- <iostream> β Handles input/output streams.
- cin β Used for input from keyboard.
- cout β Used for displaying output to screen.
β¨οΈ Syntax of cin and cout
π Output (cout)
π Input (cin)
<< insertion operator (send data to output stream)
>> extraction operator (read data from input stream)
ποΈ Basic Structure of a C++ Program
π Explanation of Each Section
- #include <iostream> β Loads input/output functions.
- using namespace std; β Allows direct use of cout/cin.
- int main() β Program starts executing from here.
- { } β Body of the main function.
- cout β Prints text.
- return 0; β Indicates successful program termination.
π Top 5 Header Files in C++ & Their Inbuilt Functions
These are the most commonly used and most important header files for beginners and intermediate level students.
1οΈβ£ <iostream> β Input/Output Stream
This header file is used for taking input and printing output.
π₯ Important Inbuilt Functions:
- cout β For output
- cin β For input
- getline() β Reads full string including spaces
2οΈβ£ <cmath> β Math Functions
Used for mathematical operations.
π₯ Important Inbuilt Functions:
- sqrt(n) β Square root
- pow(a, b) β a raised to power b
- abs(x) β Absolute value
- ceil(x) β Round upward
- floor(x) β Round downward
3οΈβ£ <string> β String Handling
Used to work with strings easily.
π₯ Important Inbuilt Functions:
- length() β Returns size of string
- substr(a, b) β Extract substring
- append() β Append string at end
- compare() β Compare two strings
4οΈβ£ <cctype> β Character Handling
Used to test and convert characters.
π₯ Important Inbuilt Functions:
- toupper(ch) β Convert to uppercase
- tolower(ch) β Convert to lowercase
- isdigit(ch) β Check if digit
- isalpha(ch) β Check alphabet
5οΈβ£ <vector> β Dynamic Arrays
Vector is one of the most powerful containers in C++.
π₯ Important Inbuilt Functions:
- push_back() β Add element
- pop_back() β Remove last element
- size() β Returns number of elements
- front() β First element
- back() β Last element
π C++ Class β Complete Beginner Friendly Notes
Classes are the heart of Object-Oriented Programming in C++. Let's understand them in the easiest way possible.
π What is a Class?
A class is a user-defined blueprint or template from which objects are created.
It contains:
- Data Members β Variables that store data
- Member Functions β Functions that operate on data
π Think of a class as a βdesignβ and an object as the βactual productβ.
π Syntax of a Class
β Ending semicolon ; is compulsory after class definition.
π Access Specifiers in C++
Access specifiers control where class members can be accessed.
1οΈβ£ public
Members are accessible anywhere in the program.
2οΈβ£ private
Members can be accessed only inside the class (default mode).
3οΈβ£ protected
Accessible inside class + child class (inheritance), but not outside.
π Creating an Object
Object is an instance of the class.
π Complete Basic Example of a Class
π§ Explanation of Above Example
- class Student β Creates a blueprint for students
- name, age, marks β Data members
- input() β Takes input from user
- display() β Shows student details
- Student s1; β Creates an object
- s1.input() β Accesses public function
πΎ Memory Concept (Easy Explanation)
- Class itself consumes **0 memory**.
- Memory is allocated **only when an object is created**.
- Each object gets a separate copy of data members.
Example:
π C++ Access Specifiers + Inheritance Syntax (Full Notes)
This note explains access levels and how they behave in inheritance in a simple, student-friendly way.
π What Are Access Specifiers?
Access specifiers decide whether class members (variables/functions) are:
- β Accessible inside the class
- β Accessible in derived class
- β Accessible outside the class (object)
There are 3 access specifiers:
- public
- private
- protected
π Access Specifier Matrix (VERY IMPORTANT)
| Access Specifier | Inside Same Class | Inside Derived Class | Outside Class (Object) |
|---|---|---|---|
| public | β Accessible | β Accessible | β Accessible |
| private | β Accessible | β Not Accessible | β Not Accessible |
| protected | β Accessible | β Accessible | β Not Accessible |
π Access after Inheritance (MOST IMPORTANT TABLE)
When a derived class inherits a base class, the access of members may change depending on inheritance mode.
| Original Access | Public Inheritance | Protected Inheritance | Private Inheritance |
|---|---|---|---|
| public | public | protected | private |
| protected | protected | protected | private |
| private | not inherited | not inherited | not inherited |
Note: Private members of base class are never inherited. Only public/protected members get inherited.
π Syntax of Deriving a Child Class
π General Syntax
access_specifier can be:
- public
- private
- protected
1οΈβ£ Public Inheritance (MOST COMMON)
Public members β remain public Protected members β remain protected
2οΈβ£ Protected Inheritance
Public + Protected β become protected in derived class.
3οΈβ£ Private Inheritance
Public + Protected β become private in derived class.