C++ File
C++ File
PRACTICAL FILE
(2022-2025)
SUBMITTED BY:
UJJVALADITYA PANDEY
ENROLLMENT NO.:
00821001922
UNDE THE GUIDANCE OF
UJJVALADITYA PANDEY
S.NO. TITLE
1 Introduction to C++
2 About Borland C++
3 Basic Program Using C++
4 Basic Addition Program Using C++
5 Basic Multiplication Program
6 Decimal Division Using C++
7 Multiplication Table Using C++
8 Increment Operator Program
9 Decrement Operator Program
10 Factorial Program Using C++
11 Positive Number Checker Program
12 Grade Checker Program
13 Array Program Using C++
14 Odd & Even Using C++
15 Switch Arithmetic Program
16 Constructor
17 Destructor
18 Inheritance
19 FOR Operator
20 Call by Reference
23 Vowels Using IF
26 Thank You
Introduction To C++
C++ is a powerful and widely-used programming
language created by Bjarne Stroustrup, a Danish
computer scientist, in the late 1970s and early
1980s. Stroustrup wanted to enhance the C
language to support the principles of OOP, such as
encapsulation, inheritance, and polymorphism. He
aimed to design a language that would enable
efficient and flexible programming while
leveraging the existing C ecosystem.
C++ has found extensive use in various domains
due to its flexibility and efficiency. It is widely
used for system software development, game
development, embedded systems, scientific and
numerical computing, graphics programming,
finance, and high-performance applications.
Popular software like operating systems, browsers,
game engines, database systems, and resource-
intensive applications are often developed using
C++.
#include<conio.h>
void main()
{
getch();
Output:
void main()
{
int a,b,c;
c = a+b;
getch();
Output:
#include<iostream.h>
#include<conio.h>
void main()
{
getch();
}
Output:
Decrementation Operator in C++
#include<iostream.h>
#include<conio.h>
void main()
{
int i = 10;
getch();
}
Output:
Basic Multiplication Program in C++
#include<iostream.h>
#include<conio.h>
void main()
{
int num1,num2,result;
cout<<" Enter a Number : ";
cin>>num1;
cout<<" Enter another Number : ";
cin>>num2;
getch();
}
Output:
Decimal Division in C++
#include<iostream.h>
#include<conio.h>
void main()
{
float dividend,divisor,quotient;
cout<<"Enter the Divident: ";
cin>>dividend;
cout<<"Enter the Divisor: ";
cin>>divisor;
getch();
}
Output
Multiplication Table in C++
#include<iostream.h>
#include<conio.h>
void main()
{
int n,i;
for(i=1;i<=10;i++)
{
cout<<n<<"*"<<i<<"="<<n*i<<endl;
}
getch();
}
Output:
Factorial Program in C++
#include <iostream.h>
#include<conio.h>
void main()
{
int x, num, factorial = 1;
Output:
Positive Integer Checker In C++
#include<iostream.h>
#include<conio.h>
void main()
{
int number;
if (number > 0)
{
cout << "The number is a positive integer." <<
endl;
}
else
{
cout << "The number is not a positive integer." <<
endl;
}
getch();
}
Output Positive :
Output Negative :
#include<iostream.h>
#include<conio.h>
void main()
{
int num;
Output :
Array Program in C++
#include<iostream.h>
#include<conio.h>
void main()
{
int x[5];
cout<<endl;
cout<<x[0]<<" "<<x[1]<<" "<<x[2]<<"
"<<x[3]<<" "<<x[4]<<endl;
getch();
Output :
C++ program to find if an integer is even or
odd or neither (0) using nested if statement
#include<iostream.h>
#include<conio.h>
void main()
{
int num;
// outer if condition
if (num != 0)
{
// inner if condition
if ((num % 2) == 0)
{
cout << "The number is even." << endl;
}
else
{
cout << "The number is odd." << endl;
}
}
Output 1:
Output 2:
Output 3:
#include<iostream.h>
#include<conio.h>
void main()
{
int i=1;
while(i<=3)
{
int j = 1;
while (j <= 3)
{
cout<<i<<" "<<j<<"\n";
j++;
}
i++;
}
getch();
}
Output :
Switch Arithmetic Program in C++
#include<iostream.h>
#include<conio.h>
void main()
{
char oper;
float num1, num2;
switch (oper)
{
case '+':
cout<<endl<<num1<< " + " <<num2<< " =
"<<num1 + num2<<endl;
break;
case '-':
cout<<endl<<num1<< " - "<<num2<< " =
"<<num1 - num2<<endl;
break;
case '*':
cout<<endl<<num1<< " * "<< num2 << "
= "<< num1 * num2<<endl;
break;
case '/':
cout <<num1 << " / "<< num2 << " = "<<
num1 / num2<<endl;
break;
default:
cout<< "Error! The operator is not correct";
break;
}
getch();
}
Outputs :
Destructor Program in C++
#include<iostream.h>
#include<conio.h>
class Student
{
private:
int rno;
char name[8];
public:
void get()
{
cout<<"\n Enter a value for Roll No. : ";
cin>>rno;
void main()
{
clrscr();
Student s1;
s1.show();
Student s2(1001);
s2.show();
Student s3;
s3.get();
s3.show();
getch();
}
Output :
Constructor Program in C++
#include<iostream.h>
#include<conio.h>
#include<string.h>
class CAM2
{
private:
int id;
char name[20];
public:
CAM2()
{
id = 0;
strcpy(name, "");
}
void Kartik()
{
cout << "\n Enter ID: ";
cin >> id;
cout << "\n Enter Name: ";
cin >> name;
}
void Ujjval()
{
cout << "\n Enter ID: ";
cin >> id;
cout << "\n Enter Name: ";
cin >> name;
}
~CAM2()
{
cout << "\n Destructor";
}
};
void main()
{
CAM2 h;
h.Ujjval();
CAM2 hh(1001,"Vidit");
hh.Ujjval();
h.Kartik();
h.Ujjval();
getch();
}
Output :
Inheritance Program in C++
#include<iostream.h>
#include<conio.h>
class Animal
{
public:
void eat()
{
cout<<"Eating..."<<endl;
}
};
class dog:public Animal
{
public:
void bark()
{
cout<<"barking...";
}
};
void main()
{
dog d1;
d1.eat();
d1.bark();
getch();
}
Output :
Call By Reference in C++
#include<iostream.h>
#include<conio.h>
//Call by Reference
void main ()
{
clrscr();
Output:
Employee Id Program in C++
#include<iostream.h>
#include<conio.h>
class Emp
{
int empid;
char name[10];
char org[15];
double sal;
public: //Access Specifier
void get()
{
cout<<"\n Enter Emp ID: ";
cin>>empid;
void show()
{
cout<<"\n Employee ID: "<<empid;
cout<<"\n Name of the Employee: "<<name;
cout<<"\n Organization, working with: "<<org;
cout<<"\n Salary of the Employee: "<<sal;
}
};
void main()
{
clrscr();
Emp e;
e.get();
e.show();
getch();
}
Output:
FOR Operator in C++
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
}
getch();
}
Output:
Vowels using IF ELSE IF in C++
#include<iostream.h>
#include<conio.h>
void main()
{
char alpha;
cout<<"Enter an alphabet:=";
cin>>alpha;
if(alpha=='a')
cout<<"It is vowel";
else if(alpha=='e')
cout<<"It is vowel";
else if(alpha=='i')
cout<<"It is vowel";
else if(alpha=='o')
cout<<"It is vowel";
else if(alpha=='u')
cout<<"It is vowel";
else
cout<<"It is consonant";
getch();
}
OUTPUT:
Vowels using IF in C++
#include<iostream.h>
#include<conio.h>
void main()
{
char alpha;
cout<<"Enter an alphabet:=";
cin>>alpha;
if(alpha=='a'||alpha=='e'||alpha=='i'||alpha=='o'||
alpha=='u')
cout<<"It is vowel";
else
cout<<"It is consonant";
getch();
}
Output:
Star Pattern Using Nested in C++
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n;
// Printing stars
cout << "* ";
getch();
}
Output:
Switch Case Program
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int choice;
cout << "Menu:\n";
cout << "1. Option 1\n";
cout << "2. Option 2\n";
cout << "3. Option 3\n";
cout << "Enter your choice (1-3): ";
cin >> choice;
switch (choice)
{
case 1:
cout << "You selected Option 1";
break;
case 2:
cout << "You selected Option 2";
break;
case 3:
cout << "You selected Option 3";
break;
default:
cout << "Invalid choice";
break;
}
getch();
}
Output:
Copy Constructor
#include<iostream.h>
#include<conio.h>
class MyClass
{
private:
int data;
public:
MyClass() // Default Constructor
{
data = 0;
}
MyClass(int d) // Parameterized Constructor
{
data = d;
}
MyClass(const MyClass& other) // Copy
Constructor
{
data = other.data;
}
void displayData()
{
cout << "Data: " << data << endl;
}
};
void main()
{
clrscr();
MyClass obj1(10);
// Creating object using parameterized constructor
MyClass obj2 = obj1;
// Creating object using copy constructor
obj1.displayData();
obj2.displayData();
getch();
}
Output:
Ternary Operator
#include <iostream.h>
#include <conio.h>
void main()
{
int number = 10;
char* result;
// Using ternary operator to assign a value based on
condition
Output:
Go To Statement
#include <iostream.h>
#include <conio.h>
void main()
{
int number;
cout << "Enter a number: ";
cin >> number;
if (number > 0)
{
goto positive;
}
else if (number < 0)
{
goto negative;
}
else
{
goto zero;
}
// Jump to the positive label
positive:
zero:
cout << "The number is zero." << endl;
goto end;
getch();
}
Output:
Multidimensional Array
#include <iostream.h>
#include <conio.h>
void main()
{
int arr[3][3];
getch();
}
Output:
Pointers
#include <iostream.h>
#include <conio.h>
void main()
{
int number;
int* ptr;
cin>>number;
ptr = &number;
getch(); }
Output:
Pointers Arithmetic
#include <iostream.h>
#include <conio.h>
void main()
{
int numbers[] = {10, 20, 30, 40, 50};
int* ptr = numbers;
for (int i = 0; i < 5; i++)
{
cout << "Element " << i << ": " << *ptr <<
endl;
ptr++;
}
getch();
}
Output:
Swap Function
#include <iostream.h>
#include <conio.h>
// Function to swap two numbers using call-by-value
void swap(int a, int b)
{
int temp = a;
a = b;
b = temp;
}
void main()
{
int num1 = 10, num2 = 20;
cout << "Before swapping: num1 = " << num1
<< ", num2 = " << num2 << endl;
swap(num1, num2);
Output:
Call By Value
#include <iostream.h>
#include <conio.h>
void main()
{
int n;
change(n);
Output:
Return By Value
#include <iostream.h>
#include <conio.h>
// Function to add two numbers and return the result by
value
int addNumbers(int a, int b)
{
int sum = a + b;
return sum;
}
void main()
{
int num1, num2;
cout<<"Enter the two numbers"<<endl;
cin>>num1>>num2 ;
// Call the addNumbers function by passing values and
store the result
cout << "The sum is: " << result << endl;
getch();
}
Output:
Call By Reference
#include <iostream.h>
#include <conio.h>
void change(int &No)
{
No=No*No;
void main()
{
int n;
change(n);
cout<<"value outside function : "<<n<<endl;
getch();
}
Output:
Return By Reference
#include <iostream.h>
#include <conio.h>
// Function to increment a number provided by the
user and return it by reference
int& increment(int& num)
{
num++;
return num;
}
void main()
{
int num;
cout << "Enter a number: ";
cin >> num;
cout << "Before increment: " << num << endl;
// Call the increment function and assign the
returned reference to a variable
int& result = increment(num);
cout << "After increment: " << result << endl;
cout << "Original value: " << num << endl
getch();
}
Output:
String Manipulation
#include <iostream.h>
#include <conio.h>
#include <string.h>
void main()
{
char str1[100], str2[100], combined[200];
Output:
Array Of Strings
#include <iostream.h>
#include <conio.h>
void main()
{
const int SIZE = 5;
char strings[SIZE][100];
// Input strings
for (int i = 0; i < SIZE; i++)
{
cout << "Enter string " << i + 1 << ": ";
cin.getline(strings[i], 100);
}
// Output strings
cout << "Entered strings:" << endl;
for (int i = 0; i < SIZE; i++)
{
cout << strings[i] << endl;
}
getch();
}
Output: