C & C++ Notes –

C & C++ Notes – Chapter 1: Introduction

(English + Hindi)


📌 What is Programming?

English:
Programming is the process of writing instructions for a computer to perform tasks.

Hindi:
Programming वह प्रक्रिया है जिसमें हम कंप्यूटर को काम करने के लिए निर्देश लिखते हैं।


📌 What is C Language?

English:
C is a powerful, structured, and middle-level programming language developed by Dennis Ritchie in 1972 at Bell Labs.

Hindi:
C एक शक्तिशाली, structured और middle-level प्रोग्रामिंग language है, जिसे Dennis Ritchie ने 1972 में Bell Labs में बनाया था।


📌 What is C++?

English:
C++ is an extension of C language with Object-Oriented Programming (OOP) features. Created by Bjarne Stroustrup in 1983.

Hindi:
C++ C भाषा का बढ़ा हुआ रूप है जिसमें Object-Oriented Programming (OOP) फीचर्स जोड़े गए हैं। इसे Bjarne Stroustrup ने 1983 में बनाया।


Key Difference: C vs C++

FeatureCC++
TypeProceduralObject-Oriented + Procedural
ApproachFunction-basedObject + Function-based
SecurityLessHigh (Encapsulation)
OOP Support❌ No✅ Yes

🧠 Flowchart: Basic Program Flow

   +---------+
   |  Start  |
   +---------+
        |
        v
+------------------+
|  Input Data      |
+------------------+
        |
        v
+------------------+
|  Process (Logic) |
+------------------+
        |
        v
+------------------+
| Output / Result  |
+------------------+
        |
        v
   +---------+
   |  End    |
   +---------+

First Program in C

#include <stdio.h>

int main() {
    printf("Hello World");
    return 0;
}

Output:

Hello World

First Program in C++

#include <iostream>
using namespace std;

int main() {
    cout << "Hello World";
    return 0;
}

Output:

Hello World

📘 Explanation (English + Hindi)

  • #include – includes library
    Hindi: लाइब्रेरी जोड़ना
  • main() – starting point of program
    Hindi: प्रोग्राम यहीं से शुरू होता है
  • printf / cout – output function
    Hindi: स्क्रीन पर मैसेज दिखाता है

📝 Exercises / अभ्यास

Task-1

Write a program to print:

My Name
My College
My Course

Hindi:
एक प्रोग्राम लिखिए जो आपका नाम, कॉलेज और कोर्स दिखाए।


Task-2

Draw a flowchart for “ATM Cash Withdrawal”.

Hindi:
ATM से पैसे निकालने का फ्लोचार्ट बनाइए।


Task-3

Difference between C and C++ (5 points)

Hindi:
C और C++ में 5 अंतर लिखिए।


✨ Ready for Next Chapter?

Available Chapters:

  1. ✅ Introduction
  2. Variables & Data Types
  3. Input / Output
  4. Operators
  5. Conditions & Branching
  6. Loops
  7. Functions
  8. Arrays
  9. Pointer vs Reference
  10. Strings
  11. Structures vs Classes
  12. Object-Oriented Concepts (for C++)


📘 Chapter-2: Variables & Data Types


What is a Variable?

English:
A variable is a container used to store data in memory.

Hindi:
Variable एक container होता है जिसमें हम memory में data store करते हैं।


Rules for Naming Variables

English

  1. Must start with a letter or underscore _
  2. No spaces allowed
  3. No special characters except _
  4. Case-sensitive (ageAge)
  5. Can’t use keywords (like int, float, etc.)

Hindi

  1. Variable नाम letter या _ से शुरू होना चाहिए
  2. Space नहीं होना चाहिए
  3. Special characters नहीं होंगे (@,#,% etc.)
  4. Capital & small अलग माने जाते हैं
  5. Keywords variable नाम नहीं हो सकते

Example Variable Declarations

C Language

int age = 20;
float marks = 85.5;
char grade = 'A';

C++ Language

int age = 20;
float marks = 85.5;
char grade = 'A';
string name = "Rahul";

🧠 Flowchart: Using a Variable

+---------+
|  Start  |
+---------+
     |
     v
+-------------+
| Declare Var |
+-------------+
     |
     v
+-------------+
| Assign Data |
+-------------+
     |
     v
+-------------+
| Use/Display |
+-------------+
     |
     v
+---------+
|  End    |
+---------+

What are Data Types?

English:
Data types define the type of data stored in a variable.

Hindi:
Data type यह बताता है कि variable में किस प्रकार का data store होगा।


🧾 Basic Data Types in C / C++

Data TypeSizeExampleDescription
int2/4 bytes10Integer (number without decimal)
float4 bytes10.5Decimal number
double8 bytes123.456Large decimal number
char1 byte'A'Single character
string (C++ only)variable"Hello"Text

Example Program (C)

#include <stdio.h>

int main() {
    int age = 21;
    float salary = 15000.50;
    char grade = 'A';

    printf("Age = %d\n", age);
    printf("Salary = %f\n", salary);
    printf("Grade = %c\n", grade);

    return 0;
}

Example Program (C++)

#include <iostream>
using namespace std;

int main() {
    int age = 21;
    double salary = 15000.75;
    char grade = 'A';
    string name = "Rohan";

    cout << "Name = " << name << endl;
    cout << "Age = " << age << endl;
    cout << "Salary = " << salary << endl;
    cout << "Grade = " << grade;

    return 0;
}

🏋️‍♂️ Practice Exercises / अभ्यास

✍️ Q1:

Declare variables for:

  • Student name
  • Roll number
  • Marks
  • Grade

(C & C++ both)


✍️ Q2:

Write a program to input age & print:

You are eligible for vote

If age >= 18


✍️ Q3:

Make a flowchart to enter 2 numbers and display their sum.


✅ Mini Quiz

1) Which of these is a valid variable?

a) 2num
b) _roll
c) my-name


2) char stores?
a) Words
b) Decimal numbers
c) Single character ✅


🎯 Output practice task

Write a program to print this:

My name is Rahul
I am 20 years old
My grade is A


📘 Chapter 3: Input / Output (I/O)

(English + Hindi)


What is Input?

English:
Input means taking information from the user.

Hindi:
Input का मतलब है यूज़र से जानकारी लेना।


What is Output?

English:
Output means displaying information/results to the user.

Hindi:
Output का मतलब है यूज़र को जानकारी/परिणाम दिखाना।


Input / Output in C

📌 Header File

#include <stdio.h>

📌 Output Function

printf("message");

📌 Input Function

scanf("%d", &variable);

📍 Example (C) — Input 2 Numbers & Print Sum

#include <stdio.h>

int main() {
    int a, b, sum;
    
    printf("Enter first number: ");
    scanf("%d", &a);
    
    printf("Enter second number: ");
    scanf("%d", &b);
    
    sum = a + b;
    
    printf("Sum = %d", sum);
    return 0;
}

Input / Output in C++

📌 Header File

#include <iostream>
using namespace std;

📌 Output Function

cout << "message";

📌 Input Function

cin >> variable;

📍 Example (C++) — Input 2 Numbers & Print Sum

#include <iostream>
using namespace std;

int main() {
    int a, b, sum;

    cout << "Enter first number: ";
    cin >> a;

    cout << "Enter second number: ";
    cin >> b;

    sum = a + b;

    cout << "Sum = " << sum;
    return 0;
}

🧠 Flowchart: Input & Output

+---------+
|  Start  |
+---------+
     |
     v
+----------------+
| Take Input A,B |
+----------------+
     |
     v
+---------------+
| Calculate A+B |
+---------------+
     |
     v
+-------------+
| Display Sum |
+-------------+
     |
     v
+---------+
|  End    |
+---------+

🎯 Format Specifiers (C Language)

Data TypeFormat SpecifierExample
int%d10
float%f20.5
char%c‘A’
string%s“Rahul”

📝 Practice Tasks

Task-1

Take input: Name, Age, City
And print:

Hello <Name>, Age: <Age>, City: <City>

Task-2

Write C & C++ program to input 3 numbers and print average.


Task-3

Flowchart to enter marks and show pass/fail.
(Passing marks > 40)


Mini MCQ Quiz

  1. Which is input in C++?
    a) printf
    b) cout
    c) cin
  2. %d is used for:
    a) decimal number ✅
    b) string
    c) character

📘 Chapter-4: Operators (C & C++)

(English + Hindi + Programs + Practice)


What is an Operator?

English:
Operators are symbols used to perform operations on values and variables.

Hindi:
Operators वे symbols होते हैं जिनका उपयोग गणना/कार्य करने के लिए किया जाता है।


Types of Operators

TypeMeaningExample
ArithmeticMaths operations+ - * / %
RelationalComparison> < >= <= == !=
LogicalTrue/False logic`&&
AssignmentAssign value= += -= *= /=
Increment / DecrementIncrease / Decrease++ --
ConditionalTernary operator?:
BitwiseBit operations`&

1) Arithmetic Operators (गणितीय ऑपरेटर)

OperatorMeaningExample
+Additiona + b
-Subtractiona - b
*Multiplicationa * b
/Divisiona / b
%Modulus (Remainder)a % b

📍 Example (C / C++ Same Logic)

int a = 10, b = 3;
printf("%d", a % b); // Output: 1
int a = 10, b = 3;
cout << a % b; // Output: 1

2) Relational Operators (तुलना करने वाले)

OperatorMeaningExample
==Equal toa == b
!=Not equala != b
>Greatera > b
<Lessa < b
>=Greater or equala >= b
<=Less or equala <= b

3) Logical Operators (तर्क वाले)

OperatorMeaningExample
&&AND(a>5 && b<10)
``
!NOT!(a>b)

4) Increment / Decrement

OperatorMeaning
++Increase value by 1
--Decrease value by 1

Example:

int x = 5;
x++; // 6

🧠 Flowchart: Operator Usage

Start
 ↓
Enter A, B
 ↓
Perform A+B
 ↓
Display Result
 ↓
End

✅ Example Program (C)

#include <stdio.h>
int main() {
    int a = 10, b = 3;
    
    printf("Addition = %d\n", a + b);
    printf("Subtraction = %d\n", a - b);
    printf("Multiplication = %d\n", a * b);
    printf("Division = %d\n", a / b);
    printf("Modulus = %d\n", a % b);
    
    return 0;
}

✅ Example Program (C++)

#include <iostream>
using namespace std;

int main() {
    int a = 10, b = 3;

    cout << "Addition = " << a + b << endl;
    cout << "Subtraction = " << a - b << endl;
    cout << "Multiplication = " << a * b << endl;
    cout << "Division = " << a / b << endl;
    cout << "Modulus = " << a % b << endl;

    return 0;
}

📝 Exercises / अभ्यास

1️⃣ Input two numbers and perform all arithmetic operations
2️⃣ Check if number > 18 (true/false)
3️⃣ Find remainder when number is divided by 5
4️⃣ Use ++ and — on a variable and print results
5️⃣ Write a flowchart for simple calculator


🎯 MCQ

  1. % operator gives:
    a) Division
    b) Remainder ✅
    c) Multiplication
  2. == checks:
    a) Equal ✅
    b) Assignment
    c) Address

📘 Chapter-5: Conditional Statements

Why Conditions?

English:
Conditions allow a program to take decisions — run different code based on true/false.

Hindi:
Conditions से प्रोग्राम निर्णय लेता है — यानी स्थिति के अनुसार अलग-अलग कोड चलता है।


Types of Conditional Statements

StatementMeaning
ifExecutes code if condition true
if-elseTrue → run one block, False → second block
else ifMultiple conditions
switchMenu-based multi-choice

if Statement

SYNTA X

if(condition) {
   // code
}

Example

Check if age ≥ 18

✅ C Program

#include <stdio.h>
int main() {
    int age;
    printf("Enter age: ");
    scanf("%d", &age);

    if(age >= 18) {
        printf("Eligible for vote");
    }
    return 0;
}

✅ C++ Program

#include <iostream>
using namespace std;

int main() {
    int age;
    cout << "Enter age: ";
    cin >> age;

    if(age >= 18) {
        cout << "Eligible for vote";
    }
    return 0;
}

if-else Statement

Example

Check even or odd number

✅ C

#include <stdio.h>
int main() {
    int n;
    printf("Enter number: ");
    scanf("%d", &n);

    if(n % 2 == 0)
        printf("Even");
    else
        printf("Odd");

    return 0;
}

✅ C++

#include <iostream>
using namespace std;

int main() {
    int n;
    cout << "Enter number: ";
    cin >> n;

    if(n % 2 == 0)
        cout << "Even";
    else
        cout << "Odd";

    return 0;
}

else if Ladder

Example — Student grade

Marks >= 90 → A  
Marks >= 75 → B  
Marks >= 50 → C  
Else → Fail

✅ C / C++

int marks;
cin >> marks;

if(marks >= 90)
    cout << "Grade A";
else if(marks >= 75)
    cout << "Grade B";
else if(marks >= 50)
    cout << "Grade C";
else
    cout << "Fail";

switch Case

Used for menu/program options.

Example — Day number to name

int day;
cin >> day;

switch(day) {
    case 1: cout<<"Monday"; break;
    case 2: cout<<"Tuesday"; break;
    case 3: cout<<"Wednesday"; break;
    default: cout<<"Invalid";
}

🧠 Flowchart — if-else (Vote Eligibility)

Start
 ↓
Enter Age
 ↓
Age ≥ 18 ?
 → Yes: Print "Eligible"
 → No: Print "Not Eligible"
 ↓
End

📝 Practice Problems

1️⃣ Input marks and print Grade (A/B/C/Fail)
2️⃣ Check positive, negative, zero
3️⃣ Menu:

1 = English
2 = Hindi
3 = Exit

4️⃣ Check divisibility by 5 & 11
5️⃣ Student pass if >= 40 else fail


🎯 MCQ

  1. if(x>10) means:
    ✔ true if x > 10
  2. Which is multi-option?
    ✔ switch

📎 Tip

Use == for compare, not =
👉 if(a==5)
👉 if(a=5)



📘 Loops

✅ What is a Loop?

English:
A loop executes a block of code repeatedly until a condition becomes false.

Hindi:
Loop बार-बार कोड चलाता है जब तक condition false न हो जाए।


✅ Types of Loops

LoopMeaningHindi
forKnown number of repetitionsनिश्चित बार के लिए
whileCondition first, then loopCondition पहले check
do-whileRuns at least onceपहले चलता है, फिर check

1️⃣ for Loop

Syntax

for(initialization; condition; increment) {
    // code
}

Example: Print 1 to 5

C

#include <stdio.h>
int main() {
    for(int i=1; i<=5; i++) {
        printf("%d\n", i);
    }
    return 0;
}

C++

#include <iostream>
using namespace std;

int main() {
    for(int i=1; i<=5; i++) {
        cout << i << endl;
    }
    return 0;
}

2️⃣ while Loop

Example: Print 1 to 5

C / C++

int i = 1;
while(i <= 5) {
    printf("%d\n", i);
    i++;
}

3️⃣ do-while Loop

Example: Print 1 to 5

int i = 1;
do {
    printf("%d\n", i);
    i++;
} while(i <= 5);

✅ Runs once even if condition false.


🧠 Flowchart: for Loop (1-5)

Start
 ↓
i = 1
 ↓
Is i <= 5?
 → Yes → Print i → i = i+1 → repeat
 → No → End

⭐ Common Loop Examples

💡 Sum of 1 to N

int n, sum=0;
cin >> n;
for(int i=1; i<=n; i++) {
    sum += i;
}
cout << sum;

💡 Print Multiplication Table

int n;
cin >> n;
for(int i=1; i<=10; i++) {
    cout << n << " x " << i << " = " << n*i << endl;
}

💡 Factorial of Number

int n, fact=1;
cin >> n;
for(int i=1; i<=n; i++) {
    fact *= i;
}
cout << fact;

⚠️ Infinite Loop

while(1) {
   // endless loop
}

🎯 Loop Uses in Real Life

  • Counting marks
  • Repeating menu in ATM
  • Printing bill items
  • Games: repeat turns

📝 Exercises

1️⃣ Print 1 to 100
2️⃣ Print all even numbers 1-50
3️⃣ Print table of 15
4️⃣ Display sum of digits of a number
5️⃣ Find factorial of a number
6️⃣ Print this pattern:

*  
* *  
* * *  
* * * *  

MCQ

1. Which loop runs at least once?
✔ do-while

2. for loop syntax order?
Initialization → Condition → Increment ✅


🧠 Tip

If you don’t know how many times to run → while
If you know count → for



What is a Function?

English:
A function is a block of code that performs a specific task and can be reused.

Hindi:
Function कोड का एक भाग होता है जो एक काम करता है और उसे बार-बार इस्तेमाल किया जा सकता है।


Why Functions? (क्यों ज़रूरी?)

AdvantageExplanation
ReusabilitySame code used multiple times
Easy DebuggingErrors easy to fix
Cleaner CodeCode becomes organized
Team WorkProgram divided into parts

Hindi:
Functions कोड को छोटा, साफ़ और समझने लायक बनाते हैं।


Function Types

TypeCC++Meaning
Built-inprintf(), scanf()cout, cinAlready defined
User DefinedProgrammer createsProgrammer createsCustom function

Function Syntax

C

returnType functionName(parameters) {
    // code
}

C++

returnType functionName(parameters) {
    // code
}

✅ Example — Function Without Return, Without Parameter

C Program

#include <stdio.h>

void hello() {
    printf("Hello World");
}

int main() {
    hello();
    return 0;
}

C++ Program

#include <iostream>
using namespace std;

void hello() {
    cout << "Hello World";
}

int main() {
    hello();
    return 0;
}

✅ Example — Function With Return & Parameters

C

#include <stdio.h>

int add(int a, int b) {
    return a + b;
}

int main() {
    int result = add(5, 3);
    printf("Sum = %d", result);
    return 0;
}

C++

#include <iostream>
using namespace std;

int add(int a, int b) {
    return a + b;
}

int main() {
    cout << "Sum = " << add(5, 3);
    return 0;
}

Flowchart — Function Call

Start
 ↓
Main Program
 ↓
Call Function
 ↓
Function Executes Task
 ↓
Return to Main
 ↓
End

✅ Function With Return, No Parameter

int getNumber() {
    return 10;
}

✅ Function No Return, With Parameters

void greet(string name) {
    cout << "Hello " << name;
}

Real-life examples of functions

Real LifeProgramming Example
Calculator buttonadd(), sub()
ATM menuwithdraw(), balanceCheck()
Mobile appslogin(), logout()

🧠 Important Notes

TermMeaning
ParameterValue received by function
ArgumentValue passed during call
ReturnSends result back

🚀 Output Example

Hello World
Sum = 8

📝 Practice Questions

1️⃣ Write a function to find square of a number
2️⃣ Create a function that prints your name
3️⃣ Create a function to check even or odd
4️⃣ Make a function to find factorial
5️⃣ Make a calculator using 4 functions: add, subtract, multiply, divide


🎯 MCQ Test

  1. Function that returns nothing
    a) int
    b) float
    c) void ✅
  2. Function parameters are written in
    a) main()
    b) function definition ✅
    c) printf

🎓 Tip

If a task repeats → Use a Function



📘 What is an Array?

English:
An array is a collection of multiple values of the same data type stored in a single variable.

Hindi:
Array एक ऐसा variable है जिसमें एक ही प्रकार के कई मान (values) store किये जाते हैं।


Why use Arrays? / Arrays क्यों?

ReasonEnglishHindi
Saves memoryOne variable stores many valuesएक variable में कई values
Easy accessIndex through valuesIndex द्वारा values मिलती हैं
Better managementAvoids many variablesकई variables बनाने की ज़रूरत नहीं

Array Declaration

Syntax

dataType arrayName[size];

Example

int marks[5];

Means: 5 integers stored


Initialize Array

C / C++

int marks[5] = {80, 75, 92, 60, 85};

Access Array Elements

printf("%d", marks[0]);  // first element

Index starts from 0

IndexValue
080
175
292
360
485

🧠 Flowchart Logic — Print Array

Start
 ↓
Declare array
 ↓
Set i = 0
 ↓
i < size?
 → Yes: print value[i], i++
 → No: End

✅ Program: Print Array (C)

#include <stdio.h>
int main() {
    int marks[5] = {80, 75, 92, 60, 85};

    for(int i = 0; i < 5; i++) {
        printf("%d\n", marks[i]);
    }
    return 0;
}

✅ Program: Print Array (C++)

#include <iostream>
using namespace std;

int main() {
    int marks[5] = {80, 75, 92, 60, 85};

    for(int i = 0; i < 5; i++) {
        cout << marks[i] << endl;
    }
    return 0;
}

Input Array from User

C

int arr[5];
for(int i=0; i<5; i++) {
    scanf("%d", &arr[i]);
}

C++

int arr[5];
for(int i=0; i<5; i++) {
    cin >> arr[i];
}

🎯 Sum of Array Elements

int arr[5] = {1,2,3,4,5}, sum=0;

for(int i=0;i<5;i++){
    sum += arr[i];
}
cout << sum;

🧠 2D Array (Matrix)

C++ Example

int a[2][2] = {{1,2},{3,4}};
cout << a[1][1]; // 4

💡 Real-Life Use

Real worldCode
Student marks listmarks[]
Shopping cartitems[]
ATM pin digitspin[]

📝 Exercises

1️⃣ Create array of 10 numbers & print
2️⃣ Find sum of 5 numbers
3️⃣ Find largest number in array
4️⃣ Input 5 marks and find average
5️⃣ Make 3×3 matrix & print values


🎯 MCQ

  1. Array index starts from?
    a) 1
    b) 0 ✅
    c) -1
  2. int a[5] stores:
    a) 5 integers ✅
    b) 4 integers
    c) Infinite integers

🔥 Tip

Use array when you need same type of data stored in group



📘 Pointers & References

✅ What is a Pointer?

English:
A pointer is a variable that stores the memory address of another variable.

Hindi:
Pointer एक variable है जो किसी दूसरे variable का memory address store करता है।


✅ Why use pointers?

ReasonEnglishHindi
Direct memory accessFaster performanceMemory पर direct काम
Pass by referenceEfficient function callsFunction में data copy नहीं
Dynamic memorymalloc / newMemory control

✅ Declaring Pointer

Syntax

int *ptr;

Example

int a = 10;
int *p = &a;
SymbolMeaning
*Pointer variable
&Address operator

✅ Memory Concept Diagram

Variable: a = 10
Address : 1001

Pointer p = &a   →  stores address 1001
*p gives value   →  10

✅ C Program: Pointer

#include <stdio.h>
int main() {
    int a = 10;
    int *p = &a;

    printf("Value = %d\n", a);
    printf("Address = %p\n", p);
    printf("Value using pointer = %d\n", *p);
    return 0;
}

✅ Pointer in C++

#include <iostream>
using namespace std;

int main() {
    int a = 10;
    int *p = &a;

    cout << "Value = " << a << endl;
    cout << "Address = " << p << endl;
    cout << "Value using pointer = " << *p;
    return 0;
}

🌟 Pointer & Function

void change(int *x){
    *x = 50;
}

int main(){
    int a = 10;
    change(&a);
    cout << a; // Output: 50
}

What is Reference? (C++ Only)

English:
Reference is an alias (another name) for a variable.

Hindi:
Reference variable किसी variable का दूसरा नाम होता है।

Syntax

int a = 10;
int &r = a;

Now r and a both refer to same memory.


✅ Example: Reference

int a = 10;
int &b = a;

b = 20;
cout << a; // Output: 20

⭐ Pointer vs Reference

FeaturePointerReference
Syntaxint *pint &r
Null allowed?YesNo
Reassign later?YesNo
Used in C?
Used in C++?

🧠 Flowchart – Pointer Concept

Start
 ↓
Declare variable a
 ↓
Pointer p = &a
 ↓
Use *p to access value
 ↓
End

📝 Practice Tasks

1️⃣ Declare int pointer & print address and value
2️⃣ Create pointer to float
3️⃣ Swap 2 numbers using pointers
4️⃣ Write C++ program using reference
5️⃣ Pointer arithmetic: increment pointer


🎯 MCQ Test

QuestionAnswer
Pointer stores?Address ✅
& means?Address of variable ✅
Reference in C?No ❌

✨ Tip

Pointers = Memory boss
References = Alias / shortcut name


📘 What is a String?

English:
A string is a sequence of characters.

Hindi:
String characters (letters, digits, symbols) का समूह होता है।

Examples: "Hello", "India", "IISDVT2025"


✅ Strings in C

🔹 C strings are character arrays

char name[20] = "Rahul";

OR

char name[20] = {'R','a','h','u','l','\0'};

\0 = Null character (string terminator)


✅ Input & Output in C

#include <stdio.h>

int main() {
    char name[30];
    printf("Enter your name: ");
    scanf("%s", name);    // stops at space

    printf("Hello %s", name);
    return 0;
}

✅ Accept string with spaces (C)

gets(name);   // NOT recommended (unsafe)
fgets(name, 30, stdin); // best method

🔧 Common String Functions (C)

FunctionMeaning
strlen(s)length of string
strcpy(a,b)copy b → a
strcat(a,b)append b to a
strcmp(a,b)compare strings

Example:

char s1[20]="Hello", s2[20]="World";
strcat(s1,s2); // s1 = HelloWorld

✅ Strings in C++

C++ provides string class (easier than C)

#include <iostream>
#include <string>
using namespace std;

int main() {
    string name;
    cout << "Enter name: ";
    getline(cin, name);

    cout << "Welcome " << name;
}

⭐ Common string functions in C++

name.length();
name.size();
name.append(" India");
name.compare(other);
name.substr(0,3);

Example:

string s="Hello";
cout << s.substr(0,2); // He

🧠 Memory View

char name[6] = "India";

I n d i a \0
0 1 2 3 4 5

🧠 Flowchart — String Input & Print

Start
 ↓
Declare string
 ↓
Input name
 ↓
Display "Hello <name>"
 ↓
End

📝 Practice Exercises

1️⃣ Take name and print

Hello <name>, welcome!

2️⃣ Input first & last name → join & print full name

3️⃣ Count characters of a string

4️⃣ Reverse string (no built-in)

5️⃣ Check palindrome
(Example: madam, level)


🎯 MCQ

QuestionAnswer
String ends with?\0
C++ string input with spaces?getline()
C string type?char array ✅

⚠️ Tip

  • C uses char array
  • C++ uses string class (best)
  • Always use fgets instead of gets


📘 What are Structures & Classes?

✅ Structure (C & C++)

English:
Structure is a user-defined datatype used to group different data types.

Hindi:
Structure ऐसा datatype है जिसमें अलग-अलग प्रकार के values को एक साथ store किया जाता है।

Example uses: student record, employee data, bank record


✅ Class (C++ Only)

English:
Class is an advanced data structure that contains data + functions (OOP concept).

Hindi:
Class एक advanced structure है जिसमें data और उस data पर काम करने वाले functions दोनों होते हैं।


🆚 Structure vs Class — Difference Table

FeatureStructureClass
LanguageC & C++Only C++
PurposeGroup dataData + Functions (OOP)
Access specifier defaultPublicPrivate
OOP support❌ No✅ Yes
SecurityLowHigh (Data hiding)

Hindi Note:
Class में security ज्यादा होती है क्योंकि data hide किया जा सकता है।


Syntax — Structure in C

#include <stdio.h>

struct Student {
    int roll;
    char name[20];
    float marks;
};

int main() {
    struct Student s1 = {101, "Rahul", 88.5};
    printf("%d %s %.2f", s1.roll, s1.name, s1.marks);
}

Syntax — Structure in C++

#include <iostream>
using namespace std;

struct Student {
    int roll;
    string name;
    float marks;
};

int main() {
    Student s1 = {101, "Rahul", 88.5};
    cout << s1.roll << " " << s1.name << " " << s1.marks;
}

Class in C++ — Example

#include <iostream>
using namespace std;

class Student {
private:
    int roll;
    float marks;

public:
    void setData(int r, float m) {
        roll = r;
        marks = m;
    }

    void display() {
        cout << "Roll: " << roll << " Marks: " << marks;
    }
};

int main() {
    Student s;
    s.setData(101, 90.5);
    s.display();
}

Key Concepts Used:

  • private data → hidden
  • public functions → access data

🧠 Flowchart — Class Use

Start
 ↓
Create Object
 ↓
Call setData()
 ↓
Call display()
 ↓
End

🌟 Real Life Analogy

Real-LifeProgramming
Student FormStructure
Student with actions (study(), attend(), marks())Class

📝 Practice Questions

1️⃣ Create structure Employee with name, id, salary
2️⃣ Take user input for 3 students & print
3️⃣ Create class Car with model, year, show() method
4️⃣ Compare structure & class (10 points)
5️⃣ Make class with input() & output() methods


🎯 Mini MCQ

QuestionAnswer
Class default accessprivate ✅
Structure stores?Multiple data types ✅
OOP supported in structure?

💡 Tip

Structure = data only
Class = data + functions (OOP)


After this chapter:
📕 Full PDF + Word File
🏁 Practice Sheets
✅ Viva Questions
🎯 Project Assignments
📂 Printable Notes Version

Just say yes/next 👇 and we continue.


📘 Chapter-12: Object-Oriented Programming (OOP) in C++

What is OOP?

English:
OOP (Object-Oriented Programming) is a programming style based on objects that contain data & functions.

Hindi:
OOP एक प्रोग्रामिंग तरीका है जिसमें प्रोग्राम objects के रूप में बनाया जाता है — जिनमें data और functions दोनों होते हैं।


🎯 4 Pillars of OOP

ConceptMeaningHindi
EncapsulationBundle data + functionsData को सुरक्षित बांधना
InheritanceDerive new class from oldगुणों का विरासत में मिलना
PolymorphismOne name, many formsएक नाम, कई रूप
AbstractionHide complexityकेवल ज़रूरी चीजें दिखाना

1. Encapsulation

Data + functions inside a class
Data security using private members

Example

#include<iostream>
using namespace std;

class Student {
private:
    int marks;

public:
    void setMarks(int m) { marks = m; }
    int getMarks() { return marks; }
};

int main() {
    Student s;
    s.setMarks(90);
    cout << s.getMarks();
}

2. Inheritance

Parent → Child

Types of Inheritance

  • Single
  • Multi-level
  • Multiple
  • Hierarchical
  • Hybrid

Example (Single Inheritance)

class A {
public:
    void showA(){ cout<<"Class A\n"; }
};

class B : public A {
public:
    void showB(){ cout<<"Class B"; }
};

int main(){
    B obj;
    obj.showA();
    obj.showB();
}

3. Polymorphism

Same function name → different behavior

Types

  • Compile-time (Function Overloading)
  • Run-time (Function Overriding)

Function Overloading

class Test {
public:
    void sum(int a,int b){ cout<<a+b; }
    void sum(float a,float b){ cout<<a+b; }
};

Function Overriding

class A { public: void show(){ cout<<"A"; } };
class B: public A { public: void show(){ cout<<"B"; } };

4. Abstraction

Show only necessary details
Hide complex details

Example: ATM
You see options → internal code hidden

Example

class Car {
public:
    void startEngine(){ cout<<"Engine Started"; }
private:
    void fuelPump(){ }
};

🌟 Object and Class Example

class Student {
public:
    string name;
    void intro() { cout << "My name is " << name; }
};

int main() {
    Student s;
    s.name = "Amit";
    s.intro();
}

🧠 OOP Real-Life Example

Real WorldOOP
CarClass
Specific carObject
Car functionsMethods
Keys lock carEncapsulation
Driver uses only steeringAbstraction

📊 Flowchart — OOP Object Creation

Start
 ↓
Define Class
 ↓
Create Object
 ↓
Call Methods
 ↓
End

🎯 Exercises

1️⃣ Create a class Bank with deposit & withdraw
2️⃣ Create Teacher (base) & SubjectTeacher (derived)
3️⃣ Function overloading: sum of 2 & 3 numbers
4️⃣ Create class Laptop → brand, price, show()
5️⃣ Create ATM program using OOP concepts


🧪 MCQ Quiz

QuestionAnswer
OOP is based on?Objects ✅
Default access in classPrivate ✅
Inheritance meaningproperties transfer ✅
Encapsulation meansdata hiding ✅

🎓 Chapter Completed!

Leave a Reply