UNIT I – C Basics, Operators, Control Statements
1. Identifier in C++: An identifier is the name given to a variable, function, array, or any other
user-defined item.
Rules: It must begin with a letter or underscore, cannot contain spaces, cannot be a keyword, and
is case-sensitive.
int marks; float totalMarks; void display();
2. Difference between Constants and Variables: A constant has a fixed value while a variable can
change during execution.
const int MAX = 100; int count = 10;
3. Operators in C++: Arithmetic operators perform mathematical operations, relational operators
compare values, logical operators combine conditions.
int a=10,b=5; if(a>b && b!=0) { cout<<a+b; }
4. sizeof() Operator: It returns the size of a variable or data type in bytes.
int a; cout<<sizeof(a);
5. Type Conversion: Implicit conversion is automatic, explicit conversion is done using casting.
int a=5; float b; b = (float)a;
6. if-else-if ladder: Used to check multiple conditions.
int marks; cin>>marks; if(marks>=90) cout<<"Grade A"; else if(marks>=75)
cout<<"Grade B"; else cout<<"Grade C";
7. switch Statement: Used to execute code based on selection.
int day; cin>>day; switch(day) { case 1: cout<<"Monday"; break; case 2:
cout<<"Tuesday"; break; default: cout<<"Invalid"; }
8. Loops: for loop, while loop, and do-while loop are used for repetition.
for(int i=1;i<=5;i++) cout<<i; int j=1; while(j<=5){ cout<<j; j++; } do{ cout<<j;
j++; }while(j<=5);
9. Prime Number Program using Loop:
int n,flag=0; cin>>n; for(int i=2;i<=n/2;i++) { if(n%i==0){ flag=1; break; } }
if(flag==0) cout<<"Prime"; else cout<<"Not Prime";
10. break and continue example:
for(int i=1;i<=20;i++) { if(i%3==0) continue; cout<<i<<" "; }
UNIT II – Arrays, Functions, Strings, Pointers
1. One-dimensional array stores elements in a single row, while two-dimensional array stores data
in table form.
int a[5]; int b[3][3];
2. Array Declaration and Initialization:
int a[3]={1,2,3}; float f[2]={1.5,2.5}; char c[4]={'A','B','C','\0'};
3. Sum of Array Elements Program:
int a[5]={1,2,3,4,5},sum=0; for(int i=0;i<5;i++) sum+=a[i]; cout<<sum;
4. Function Prototyping: It informs the compiler about function before its use.
int add(int,int); int main(){ add(2,3); } int add(int a,int b){ return a+b; }
5. Recursive Factorial Function:
int fact(int n) { if(n==1) return 1; else return n*fact(n-1); }
6. Strings using std::string:
#include <string> string s1="Hello"; string s2="World";
7. String Concatenation Program:
string s3 = s1 + s2; cout<<s3;
8. Pointer Arithmetic:
int a[3]={10,20,30}; int *p=a; cout<<*(p+1);
9. Passing Array to Function using Pointer:
void display(int *a) { for(int i=0;i<3;i++) cout<<a[i]; }
10. Call by Value vs Call by Reference:
void swap(int a,int b); // value void swap(int &a;,int &b;); // reference
UNIT III – OOP Concepts
Object-Oriented Programming uses classes and objects to represent real-world entities. Concepts
include encapsulation, inheritance, and polymorphism.
class Student { int roll; public: void set(int r){ roll=r; } };
UNIT IV – Inheritance and Polymorphism
Inheritance allows a class to acquire properties of another class.
class A{ public: void show(){ cout<<"A"; } }; class B: public A{};
UNIT V – Advanced C++
Dynamic memory allocation uses new and delete keywords.
int *p = new int; delete p;