0% found this document useful (0 votes)
16 views97 pages

C++ File

Uploaded by

Byg Basher
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views97 pages

C++ File

Uploaded by

Byg Basher
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 97

Object Oriented Programming using C++ LAB

PRACTICAL FILE

SUBMITTED IN THE PARTIAL FULFILLMENT OF THE


DEGREE OF

BACHELOR OF BUSINESS ADMINISTRATION(CAM)

(2022-2025)

SUBMITTED BY:
UJJVALADITYA PANDEY
ENROLLMENT NO.:
00821001922
UNDE THE GUIDANCE OF

Dr. Mahesh Sharma

(Vice Principal, BBA 1st Shift)

IDEAL INSTITUTE OF MANAGEMENT & TECHNOLOGY

(AFFILIATED TO GURU GOBIND SINGH INDRAPRASTHA UNIVERSITY,


DELHI
ACKNOWLEDGEMENT

I am preparing this practical file of Object Oriented Programming


using C++ Lab for the program of Bachelor of Business
Administration (CAM) for Ideal Institute of Management &
Technology, Affiliated to Guru Gobind Singh Indraprastha
University.

It has been a great challenge but a plenty of learning and opportunities


to gain a huge amount of knowledge on the way of preparing this file.
I could not have completed my work without the constant guidance of
Dr. Mahesh Sharma, my faculty, who helped me along the way and
was always prepared to give me feedback and guidelines whenever I
needed it.

UJJVALADITYA PANDEY

Enroll. no.: 00821001922


TABLE OF CONTENT

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

21 Employee ID program Using C++

22 Vowels Using IF ELSE IF

23 Vowels Using IF

24 Nested While Program Using C++

25 Star Pattern Nested

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++.

About Borland C++


The text editor that we are using in our C++ Lab is
Borland C++ it is a text editor and integrated
development environment (IDE) that was popular
during the late 1980s and 1990s. It was developed
by Borland International and provided a powerful
and user-friendly environment for writing,
compiling, and debugging C and C++ programs.

Borland C++ played a significant role in


popularizing C++ as a programming language and
encouraging the adoption of object-oriented
programming practices. It provided developers
with an accessible and productive environment for
harnessing the capabilities of C++.

Basic Program In C++


#include<iostream.h>

#include<conio.h>

void main()
{

cout<<"\n Hi, Ujjval this side ! ";

getch();

Output:

Basic Addition Program in C++


#include<iostream.h>
#include<conio.h>

void main()
{

int a,b,c;

cout<<"\n Enter a Value for A: ";


cin>>a;

cout<<"\n Enter a Value for B : ";


cin>>b;

c = a+b;

cout<<"\n Value of A: "<<a;

cout<<"\n Value for B: "<<b;


cout<<"\n Addition of A & B is : "<<c;

getch();

Output:

Incrementation Operator in C++

#include<iostream.h>
#include<conio.h>

void main()
{

for(int i = 0; i<=9; i++)

cout<<"\n TOKYO REVENGERSSS"<<i;

getch();

}
Output:
Decrementation Operator in C++
#include<iostream.h>
#include<conio.h>

void main()
{
int i = 10;

for (; i >= 0; i--)


{
cout << "Countdown: " << i << endl;
}

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;

result = num1 * num2;

cout << "Multiplication Result: " << result<<endl;

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;

quotient = dividend / divisor;

cout << "Division Quotient: " << quotient <<


endl;

getch();
}

Output
Multiplication Table in C++

#include<iostream.h>
#include<conio.h>

void main()
{
int n,i;

cout<<"\n Enter a variable/value : ";


cin>>n;
cout<<"The Multiplication Table for "<<n<<" is as
follows: "<<endl;

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;

cout <<"\n Type a positive number: ";


cin >> num;

for (x = 1; x <= num; x++)


{
factorial *= x; // factorial = factorial * x;
}
cout << "Factorial of " << num << " = " <<
factorial;
getch();}

Output:
Positive Integer Checker In C++

#include<iostream.h>
#include<conio.h>

void main()
{

int number;

cout << "Enter an Integer: ";


cin >> 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 :

Grade Checker Using IF and Else Operator


in C++

#include<iostream.h>
#include<conio.h>

void main()
{
int num;

cout<<"Enter a number to check grade:";


cin>>num;

if (num <0 || num >100)


{
cout<<"wrong number";
}
else if(num >= 0 && num < 50)
{
cout<<"Fail !!!";
}
else if (num >= 50 && num < 60)
{
cout<<"D Grade !";
}
else if (num >= 60 && num < 70)
{
cout<<"C Grade !";
}
else if (num >= 70 && num < 80)
{
cout<<"B Grade !";
}
else if (num >= 80 && num < 90)
{
cout<<"A Grade !";
}
else if (num >= 90 && num <= 100)
{
cout<<"A+ Grade !!! \n EXCELLENT";
}
getch();
}

Output :
Array Program in C++

#include<iostream.h>
#include<conio.h>

void main()
{
int x[5];

cout<<"\n Enter a Value for X[0] : ";


cin>>x[0];

cout<<"\n Enter a Value for X[1] : ";


cin>>x[1];

cout<<"\n Enter a Value for X[2] : ";


cin>>x[2];

cout<<"\n Enter a Value for X[3] : ";


cin>>x[3];

cout<<"\n Enter a Value for X[4] : ";


cin>>x[4];

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;

cout << "Enter an integer: ";


cin >> num;

// outer if condition
if (num != 0)
{

// inner if condition
if ((num % 2) == 0)
{
cout << "The number is even." << endl;
}

// inner else condition

else
{
cout << "The number is odd." << endl;
}
}

// outer else condition


else
{
cout << "The number is 0 and it is neither
even nor odd." << endl;
}
getch();
}

Output 1:

Output 2:
Output 3:

Nested While Program in C++

#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;

cout <<"\n Enter an operator (+, -, *, /): ";


cin >> oper;
cout <<"\n Enter a value for num1: ";
cin>>num1;

cout <<"\n Enter a value for num2: ";


cin>>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:

Student() //Default Constructor


{
}

Student(int r) //Parameterized Constructor


{
rno = r;
}

void get()
{
cout<<"\n Enter a value for Roll No. : ";
cin>>rno;

cout<<"\n Enter a value for Name : ";


cin>>name;
}
void show()
{
cout<<"\n Roll No. : "<<rno;
cout<<"\n Name : "<<name;
}
~Student() //Destructor...
{
cout<<"\n Destructor called successfully...";
}
};

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, "");
}

CAM2(int i, char n[20])


{
id = i;
strcpy(name, n);
}

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 swap(int &x, int &y)


{
int temp;

temp = x; /* save the value of x */


x = y; /* put y into x */
y = temp; /* put x into y */
}

void main ()
{
clrscr();

// local variable declaration:


int a = 100;
int b = 200;

cout << "Before swap, value of a :" << a <<


endl;
cout << "Before swap, value of b :" << b <<
endl;

// calling a function to swap the values.


swap(a, b);

cout << "After swap, value of a :" << a << endl;


cout << "After swap, value of b :" << b << endl;
getch();
}

Output:
Employee Id Program in C++

#include<iostream.h>
#include<conio.h>

class Emp
{

private: //Access Specifier

int empid;
char name[10];
char org[15];
double sal;
public: //Access Specifier

void get()
{
cout<<"\n Enter Emp ID: ";
cin>>empid;

cout<<"\n Enter Name: ";


cin>>name;

cout<<"\n Enter Organization: ";


cin>>org;

cout<<"\n Enter Salary: ";


cin>>sal;
}

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();

for(int i = 0; i<=9; i++)

cout<<"\n Hi, it's Number : "<<i;

}
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;

cout<<"\n Enter a value: ";


cin>>n;
for (int i = 0; i < n; i++)
{

// Inner loop to handle number of columns


// values changing acc. to outer loop
for (int j = 0; j <= i; j++)
{

// Printing stars
cout << "* ";

// Ending line after each row


cout << endl;
}

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

result = (number > 5) ? "Greater than 5" : "Less


than or equal to 5";

cout << result << endl;


getch();
}

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:

cout << "The number is positive." << endl;


goto end;

// Jump to the negative label


negative:

cout << "The number is negative." << endl;


goto end;

// Jump to the zero label

zero:
cout << "The number is zero." << endl;
goto end;

// End of the program


end:
cout << "Program ended." << endl;

getch();
}

Output:
Multidimensional Array

#include <iostream.h>
#include <conio.h>
void main()
{
int arr[3][3];

cout << "Enter the elements of the array:" <<


endl;

for (int i = 0; i < 3; i++)


{
for (int j = 0; j < 3; j++)
{
cout << "Enter element at position
[" << i << "][" << j << "]: " ;
cin >> arr[i][j];
}
}
cout << "Array elements:" << endl;

for (int i = 0; i < 3; i++)


{
for (int j = 0; j < 3; j++)
{
cout << arr[i][j] << " ";
}
cout << endl;
}

getch();
}

Output:
Pointers

#include <iostream.h>

#include <conio.h>

void main()
{

int number;

int* ptr;

cout<<"\n enter number";

cin>>number;

ptr = &number;

cout << "Value of number: " << *ptr << endl;

cout << "Memory address of number: " << ptr


<< endl;

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;

// Call the swap function by passing values

swap(num1, num2);

cout << "After swapping: num1 = " << num1 <<


", num2 = " << num2 << endl;
getch();
}

Output:

Call By Value

#include <iostream.h>
#include <conio.h>

void change(int No)


{
No = No * No;
cout << "Modified Number: " << No << endl;
}

void main()
{
int n;

cout << "Enter a value: ";


cin >> n;

cout << "Original Number: " << n << endl;

change(n);

cout << "Original Number outside the function:


" << n << endl;
getch();
}

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

int result = addNumbers(num1, num2);

cout << "The sum is: " << result << endl;
getch();
}

Output:

Call By Reference

#include <iostream.h>
#include <conio.h>
void change(int &No)
{
No=No*No;

cout<<"MODIFIED VALUE : "<<No<<endl;


}

void main()
{

int n;

cout<<"enter a number :"<<endl;


cin>>n;

cout<<"original value : "<<n<<endl;

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];

cout << "Enter the first string: ";


cin.getline(str1, 100);

cout << "Enter the second string: ";


cin.getline(str2, 100);

// Combine the two strings


strcpy(combined, str1);
strcat(combined, " ");
strcat(combined, str2);
cout << "Combined string: " << combined <<
endl;

// Find the length of the first string


int length = strlen(str1);
cout << "Length of the first string: " << length
<< endl;

// Compare the two strings


int comparisonResult = strcmp(str1, str2);
if (comparisonResult == 0)
{
cout << "The strings are equal" << endl;
}
else if (comparisonResult < 0){
cout << "The first string is smaller than the
second string" << endl;
}
else
{
cout << "The first string is greater than the
second string" << endl;
}
getch();
}

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:

You might also like