Introduction to C++ Programming
1. Introduction to C++
Developed by Bjarne Stroustrup as an extension of C, with added object-oriented features.
Key Features: Multi-paradigm (procedural, object-oriented, generic), efficient, and close to
hardware.
2. Basic Syntax
Structure:
cpp
Copy code
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!";
return 0;
#include <iostream>: Imports standard input-output library.
using namespace std;: Uses the standard library namespace.
3. Data Types and Variables
Basic Types: int, float, double, char, bool.
Declaration: int age = 25; or double salary = 45000.75;.
4. Operators
Arithmetic: +, -, *, /, %.
Relational: ==, !=, <, >, <=, >=.
Logical: &&, ||, !.
Assignment: =, +=, -=, etc.
5. Control Structures
Conditional Statements:
o if, else, switch.
Loops:
o for: for (int i = 0; i < n; i++).
o while and do-while: Repeats based on condition.
6. Functions
Define reusable code blocks.
Example:
cpp
Copy code
int add(int a, int b) { return a + b; }
Call by Value vs. Call by Reference (using &).
7. Object-Oriented Programming (OOP)
Classes and Objects:
o Class: A blueprint (e.g., class Car {...};).
o Object: An instance of a class.
Key Concepts:
o Encapsulation: Bundles data and methods.
o Inheritance: Reuses code by extending classes.
o Polymorphism: Allows different behaviors in derived classes.
o Abstraction: Hides complexity and shows only necessary details.
SAMPLE C++ PROGRAMS
1. C++ Program to Calculate the Sum and Average of Numbers
#include <iostream>
using namespace std;
int main(){
int x,y,sum;
float average;
cout << "Enter 2 integers : " << endl;
cin>>x>>y;
sum=x+y;
average=sum/2;
cout << "The sum of " << x << " and " << y << " is " << sum << "." << endl;
cout << "The average of " << x << " and " << y << " is " << average << "." << endl;
2. C++ program to find the area of a triangle based on its base and height
#include<iostream>
using namespace std;
int main()
float b, h, area;
cout<<"Enter Base length of Triangle: ";
cin>>b;
cout<<"Enter Height length of Triangle: ";
cin>>h;
area = 0.5*b*h;
cout<<"\nArea = "<<area;
cout<<endl;
return 0;
}
3. C++ program to find and print the price to be paid after applying the discount
#include<iostream>
using namespace std;
int main()
float amount, discount, topaid;
cout<<"Enter Total Amount: ";
cin>>amount;
if(amount<=100)
cout<<"Amount to be Paid: "<<amount;
else
if(amount>100 && amount<=200)
discount = (amount*5)/100;
topaid = amount-discount;
else if(amount>200 && amount<=400)
discount = (amount*10)/100;
topaid = amount-discount;
else if(amount>400 && amount<=800)
discount = (amount*20)/100;
topaid = amount-discount;
else
{
discount = (amount*25)/100;
topaid = amount-discount;
cout<<"Amount to be Paid: "<<topaid;
cout<<endl;
return 0;
4. while loop C++ Program
// C++ Program to print numbers from 1 to 5
#include <iostream>
using namespace std;
int main() {
int i = 1;
// while loop from 1 to 5
while (i <= 5) {
cout << i << " ";
++i;
}
return 0;
}
5. for loop C++ Program
// C++ program to illustrate for loop to print numbers from
// 1 to n
#include <iostream>
using namespace std;
int main()
{
// initializing n (value upto which you want to print
// numbers
int n = 5;
int i; // initialization of loop variable
for (i = 1; i <= n; i++) {
cout << i << " ";
return 0;
6. C++ program to create a Calculator using the switch Statement
// Program to build a simple calculator using switch Statement
#include <iostream>
using namespace std;
int main() {
char oper;
float num1, num2;
cout << "Enter an operator (+, -, *, /): ";
cin >> oper;
cout << "Enter two numbers: " << endl;
cin >> num1 >> num2;
switch (oper) {
case '+':
cout << num1 << " + " << num2 << " = " << num1 + num2;
break;
case '-':
cout << num1 << " - " << num2 << " = " << num1 - num2;
break;
case '*':
cout << num1 << " * " << num2 << " = " << num1 * num2;
break;
case '/':
cout << num1 << " / " << num2 << " = " << num1 / num2;
break;
default:
// operator is doesn't match any case constant (+, -, *, /)
cout << "Error! The operator is not correct";
break;
return 0;
7. C++ program using array to find average marks obtained by 5 students
#include<iostream>
using namespace std;
int main()
int i, mark[5];
float sum=0, avg, perc;
cout<<"Enter Marks obtained in 5 Subjects: ";
for(i=0; i<5; i++)
cin>>mark[i];
sum = sum+mark[i];
avg = sum/5;
perc = (sum/500)*100;
cout<<"\nAverage Marks = "<<avg;
cout<<"\nPercentage Marks = "<<perc<<"%";
cout<<endl;
return 0;