Lab Manual C++ SCS102
Lab Manual C++ SCS102
Of
BCA-II Sem.
(Even 2024)
GNA UNIVERSITY
M1: To provide state of art infrastructure and conducive environment for budding
IT professionals.
M2: To establish strong industry academia relationship to enhance the technical
skills of the students and make them readily employable.
M3: To provide exposure to the emerging and establish tools and technology in
the field of computer applications.
M4: To develop curriculum in accordance with the industry requirements.
Program Educational Objectives (PEO):
Program Outcomes (PO’s) represents the knowledge, skills and attitudes the
students should have at the end of a program. The graduates will be able to
PO4: Modern Tool Usage: To identify, select and use a modern scientific and IT
tool or technique for modelling, prediction, data analysis and solving problems in
the area of computer science and making them to develop applications.
PO6: Computing Skills: Analyze a problem and identify and define the
computing requirements appropriate to its solution.
PO13: Lifelong Learning: Work as teams to build software systems and apply
the technologies in various fields of Computer Applications, including hardware
problems, Website development and management, databases, and software
engineering techniques.
Program Specific Outcomes (PSO):
PSO1: Professional Skills: Attain the ability to design and develop computer
applications, evaluate and recognize potential risks and provide innovative
solutions.
PSO4: Problem Solving: Ability to use knowledge gained for solving complex
problems using Computational sciences.
Index
Description:-
This program prints the statement “Hello World” in the C language.
Source Code:-
#include<stdio.h>
int main(){
printf("Hello World");
return 0;
}
Output:-
Page - 1
Practical – 02
(Simple Program on printing “Hello World” and “Hello Name” where name is the
input from the user)
Description:-
This program prints the statement “Hello World” and the user’s name in the C++.
Source Code:-
#include<iostream>
#include<string>
using namespace std;
int main(){
string name;
cout<<"Enter your name."<<endl;
cin>>name;
cout<<"Hello World"<<endl;
cout<<"Hello "<<name<<endl;
return 0;
}
Output:-
Page - 2
Practical – 03
(Program to check whether the given number is even or odd)
Description:-
This program checks whether the number entered by the user is even or odd and then prints the corresponding
statement.
Source Code:-
#include<iostream>
using namespace std;
int main(){
int num;
cout<<"Enter a number."<<endl;
cin>>num;
if(num%2==0)
cout<<num<<" is an even number."<<endl;
else
cout<<num<<" is an odd number."<<endl;
return 0;
}
Output:-
Page - 3
Practical – 04
(Program to check whether the given number is Armstrong or not)
Description:-
This program checks whether the number entered by the user is an Armstrong number or not and then prints
the corresponding statement.
Source Code:-
#include<iostream>
using namespace std;
int main(){
int num,ori,rem,sum=0;
cout<<"Enter a three digit number."<<endl;
cin>>num;
ori=num;
while(ori!=0){
rem=ori%10;
sum=sum+(rem*rem*rem);
ori/=10;
}
if(num==sum){
cout<<num<<" is an Armstrong number."<<endl;
}
else{
cout<<num<<" is not an Armstrong number."<<endl;
}
return 0;
}
Output:-
Page - 4
Practical – 05
(Program that computes the simple interest and compound interest payable on
principal amount (in Rs.) of loan borrowed by the customer from a bank for a given
period of time (in years) at specific rate of interest. Further determine whether the
bank will benefit by charging simple interest or compound interest)
Description:-
This program takes the principle amount, rate of interest and time in years to print the simple and the
compound interest.
Source Code:-
#include<iostream>
#include<cmath>
using namespace std;
int main(){
int principle, time;
float rate;
cout<<"Enter the Principle Amount : ";
cin>>principle;
cout<<"Enter the Rate of Interest : ";
cin>>rate;
cout<<"Enter the Time : ";
cin>>time;
Output:-
Page - 5
Practical – 06
(Program using functions by passing values call by values method)
Description:-
This program demonstrates the function call by value method. In this method, the copy of the actual parameters
is passed to the function that’s why there’s no effect even if we change the value of the parameters within the
function means It will have no effect on the original values.
Source Code:-
#include<iostream>
using namespace std;
int main(){
int num;
cout<<"Enter a number : ";
cin>>num;
Output:-
Page - 6
Practical – 07
(Program using functions by passing call by reference method.)
Description:-
This program demonstrates the function call by reference method. In this method, the address of the actual
parameters is passed to the function that’s why the effect can be seen on the original value when we alter the
parameters within the function means It will also effect the original values.
Source Code:-
#include<iostream>
using namespace std;
int main(){
int num;
cout<<"Enter a number : ";
cin>>num;
Output:-
Page - 7
Practical – 08
(Program to calculate the factorial of a number using recursion function)
Description:-
This program helps us to find the factorial of a number with the help of recursion function. Recursion is the
process in which a function recalls itself again and again to fulfill a certain task. Break or a limit must be used
to stop the function from the unnecessary calling.
Source Code:-
#include<iostream>
using namespace std;
int main(){
int f;
cout<<"Enter a number : ";
cin>>f;
Output:-
Page - 8
Practical – 09
(Program which stores the marks of 5 students in an array and then print all the
marks. The marks of students are 75, 85, 80, 95 and 90)
Description:-
This program stores the marks of the students in an array then prints it.
Source Code:-
#include<iostream>
using namespace std;
int main(){
int arr[5] = {75, 85, 80, 95, 90};
Output:-
Page - 9
Practical – 10
(Program which initializes a two-dimensional array and then print all the elements in a
matrix form)
Description:-
This program demonstrates a two-dimensional array and prints a matrix by using nested looping.
Source Code:-
#include<iostream>
using namespace std;
int main(){
int rows = 3, cols = 3;
int arr[rows][cols] = {{10, 20, 30}, {40, 50, 60}, {70, 80, 90}};
cout<<"Matrix Elements"<<endl<<endl;
Output:-
Page - 10
Practical – 11
(Program to generate all the prime numbers between 1 and n, where n is a value
supplied by the user)
Description:-
This program prints all the prime numbers that lies within a certain range. It takes the range from the user.
Source Code:-
#include<iostream>
using namespace std;
int main(){
int range, i, j;
cout<<"Enter the range till where you want to get the prime numbers : ";
cin>>range;
Output:-
Page - 11
Practical – 12
(Program to find both the largest and smallest number in a list of integers)
Description:-
This program finds the largest and smallest number from an array then prints them.
Source Code:-
#include<iostream>
using namespace std;
int main(){
int size, min, max;
cout<<"Enter the size of Array : ";
cin>>size;
int arr[size];
cout<<endl<<"Enter the Elements."<<endl<<endl;
for(int i = 0; i < size; i++){
cout<<"Element "<<(i+1)<<" : ";
cin>>arr[i];
}
max = arr[0];
for(int i = 0; i < size; i++){
if(max<arr[i])
max = arr[i];
}
min = arr[0];
for(int i = 0; i < size; i++){
if(min>arr[i])
min = arr[i];
}
cout<<endl<<"The Greatest number is "<<max;
cout<<endl<<"The Smallest number is "<<min<<endl;
}
Output:-
Page - 12
Practical – 13
(Program Illustrating Class Declarations, Definition, and Accessing Class Members)
Description:-
This program demonstrates the class declaration, definition, initializing data members, defining member
functions, and later creating objects to access all data members and member functions from within the class.
It takes the student information and then calculates the average marks and then prints it.
Source Code:-
#include <iostream>
#include<conio.h>
using namespace std;
class StudentClass {
char name[20];
int regNo, sub1, sub2, sub3;
float total, avg;
public:
void read() {
cout << "Enter Name : ";
cin >> name;
void sum() {
total = sub1 + sub2 + sub3;
avg = total / 3;
}
void print() {
cout<<endl<<"Student Details are as follow :- "<<endl;
cout << "Name :" << name << endl;
cout << "Registration Number :" << regNo << endl;
Page - 13
cout << "Marks :" << sub1 << " , " << sub2 << " , " << sub3 << endl;
cout << "Total :" << total << endl;
cout << "Average :" << avg << endl;
}
};
int main() {
StudentClass stu1;
cout << "Read and Print Student Information Class Example Program In C++"<<endl<<endl;
stu1.read();
stu1.sum();
stu1.print();
}
Output:-
Page - 14
Practical – 14
(Program to illustrate default constructor, parameterized constructor and copy
constructors)
Description:-
This program demonstrates the constructor and it’s types.
14.1 Default Constructor:- A constructor in C++ is a special member function of a class that is
automatically called when an object of that class is created. Its primary purpose is to initialize the
object's data members and prepare it for use.
Source Code:-
#include<iostream>
using namespace std;
class construct{
public:
construct(){
cout<<"A Constructor was Created.";
}
};
int main(){
construct c;
}
Output :-
Page - 15
14.2 Parameterized Constructor:- A parameterized constructor is a constructor in a class that takes one or
more parameters. It allows you to initialize the object with specific values at the time of creation.
Source Code:-
#include<iostream>
using namespace std;
class ladder{
public:
ladder(int len){
cout<<"The Length of the Ladder is "<<len<<" meters.";
}
};
int main(){
ladder l(20);
}
Output:-
Page - 16
14.3 Copy Constructor:- A copy constructor is a special constructor in C++ that creates a new object as a
copy of an existing object of the same class. It allows you to create a new object initialized with the
same values as another object.
Source Code:-
#include<iostream>
using namespace std;
class ladder{
int length;
public:
ladder(int len){
length = len;
cout<<"The Length of the Ladder is "<<len<<" meters."<<endl;
}
int main(){
ladder l1(20);
ladder l2 = l1;
}
Output:-
Page - 17
Practical – 15
(Develop a C++ program to demonstrate the destructors)
Description:-
This program creates a constructor and later destroys it with a destructor.
Source Code:-
#include <iostream>
using namespace std;
class MyClass {
public:
MyClass() {
cout << "Constructor called" << endl;
}
~MyClass() {
cout << "Destructor called" << endl;
}
};
int main() {
MyClass obj;
}
Output:-
Page - 18
Practical – 16
(Write a program to show the allocation and deallocation of memory using the new and
delete keyword)
Description:-
This program creates memory space using new keyword and destroys the memory space using the delete
keyword.
Source Code:-
#include <iostream>
using namespace std;
int main() {
int *ptr = new int;
*ptr = 42;
cout << "Value After Allocation: " << *ptr << endl;
delete ptr;
cout << "Value After Deallocation: " << *ptr << endl;
}
Output:-
Page - 19
Practical – 17
(Program to create student class, read and print N student’s details)
Description:-
This program demonstrates a student class and has two functions which get and print the student details.
Source Code:-
#include <iostream>
#include<conio.h>
using namespace std;
class StudentClass {
char name[20];
int regNo, sub1, sub2, sub3;
float total, avg;
public:
void read() {
cout << "Enter Name : ";
cin >> name;
void sum() {
total = sub1 + sub2 + sub3;
avg = total / 3;
}
void print() {
cout<<endl<<"Student Details are as follow :- "<<endl;
cout << "Name :" << name << endl;
cout << "Registration Number :" << regNo << endl;
cout << "Marks :" << sub1 << " , " << sub2 << " , " << sub3 << endl;
cout << "Total :" << total << endl;
Page - 20
cout << "Average :" << avg << endl;
}
};
int main() {
StudentClass stu1;
cout << "Read and Print Student Information Class Example Program In C++"<<endl<<endl;
stu1.read();
stu1.sum();
stu1.print();
}
Output:-
Page - 21
Practical – 18
(Program to Demonstrate the Operator Overloading and Function Overloading)
Description:-
In this program, we are going to demonstrate the operator and function overloading.
18.1. Operator Overloading:- Operator overloading in C++ allows you to define how operators behave
with user-defined types.
Source Code:-
#include <iostream>
using namespace std;
class Count {
private:
int value = 5;
public:
void operator ++ () {
++value;
}
void display() {
cout << "Count: " << value << endl;
}
};
int main() {
Count count1;
++count1;
count1.display();
}
Output:-
Page - 22
18.2. Function Overloading:- Function overloading in C++ allows you to define multiple functions with
the same name but different parameter lists.
Source Code:-
#include <iostream>
using namespace std;
int main() {
cout << "Sum of two integers: " << add(10, 20) << endl;
cout << "Sum of three integers: " << add(10, 20, 30) << endl;
}
Output:-
Page - 23
Practical – 19
(Program to Demonstrate Friend Function)
Description:-
In this program, we are going to demonstrate the friend function and access the private member of the class
with it.
Source Code:-
#include <iostream>
using namespace std;
class Number {
private:
int num = 100;
public:
friend void shownum(Number);
};
int main() {
Number n1;
shownum(n1);
}
Output:-
Page - 24
Practical – 20
(Program to Generate Fibonacci Series use Constructor to Initialize the Data
Members)
Description:-
In this program, we are going to find the Fibonacci series using a constructor.
Source Code:-
#include <iostream>
using namespace std;
class Fibonacci {
public:
Fibonacci(int size) {
int a = 0, b = 1;
cout << "Fibonacci Series:" << endl;
cout << a << " " << b << " ";
for (int i = 2; i < size; ++i) {
int next = a + b;
cout << next << " ";
a = b;
b = next;
}
cout << endl;
}
};
int main() {
Fibonacci fib(10);
}
Output:-
Page - 25
Practical – 21
(Program to implement the concept of single inheritance)
Description:-
This program demonstrates the concept of single inheritance. Single inheritance is a concept in object-
oriented programming where a class can inherit attributes and behaviours from only one parent class.
Source Code:-
#include <iostream>
using namespace std;
class Parent {
public:
void Method1() {
cout << "This is the parent method." << endl;
}
};
class Child : public Parent {
public:
void Method2() {
cout << "This is the child method." << endl;
}
};
int main() {
Child Obj;
Obj.Method1();
Obj.Method2();
}
Output:-
Page - 26
Practical – 22
(Program to implement the concept of multi-level inheritance)
Description:-
This program demonstrates the concept of multi-level inheritance. Multi-level inheritance is a type of
inheritance in object-oriented programming where a derived class (subclass) inherits from another derived
class, creating a hierarchical structure of classes.
Source Code:-
#include <iostream>
using namespace std;
class Grandfather {
public:
void displayGrandfather() {
cout << "Grandfather" << endl;
}};
class Father : public Grandfather {
public:
void displayFather() {
cout << "Father" << endl;
}};
class Child : public Father {
public:
void displayChild() {
cout << "Child" << endl;
}};
int main() {
Child child;
child.displayGrandfather();
child.displayFather();
child.displayChild();
}
Output:-
Page - 27
Practical – 23
(Program to implement the concept of multiple inheritance)
Description:-
This program demonstrates the concept of multiple inheritance. Multiple inheritance is a feature in object-
oriented programming languages where a class can inherit attributes and behaviours from more than one
base class.
Source Code:-
#include <iostream>
using namespace std;
class A {
public:
void methodA() {
cout << "Method from class A" << endl;
}};
class B {
public:
void methodB() {
cout << "Method from class B" << endl;
}};
class C : public A, public B {
public:
void methodC() {
cout << "Method from class C" << endl;
}};
int main() {
C obj;
obj.methodA();
obj.methodB();
obj.methodC();
}
Output:-
Page - 28
Practical – 24
(Program to implement the concept of hierarchical inheritance)
Description:-
This program demonstrates the concept of hierarchical inheritance. Hierarchical inheritance is a type of
inheritance in object-oriented programming where multiple derived classes inherit from a single base or
parent class.
Source Code:-
#include <iostream>
using namespace std;
class Parent {
public:
void displayParent() {
cout << "Parent" << endl;
}};
class Son : public Parent {
public:
void displaySon() {
cout << "Son" << endl;
}};
class Daughter : public Parent {
public:
void displayDaughter() {
cout << "Daughter" << endl;
}};
int main() {
Son son;
Daughter daughter;
son.displayParent();
son.displaySon();
daughter.displayParent();
daughter.displayDaughter();
}
Output:-
Page - 29
Practical – 25
(Program to implement the concept of hybrid inheritance)
Description:-
This program demonstrates the concept of hybrid inheritance. Hybrid inheritance is a combination of
multiple types of inheritance within a single class hierarchy. It can include any combination of single
inheritance, multiple inheritance, hierarchical inheritance, or multilevel inheritance.
Source Code:-
#include <iostream>
using namespace std;
class Shape {
public:
void draw() {
cout << "Shape is drawn" << endl;
}
};
int main() {
Square square;
square.Rectangle::draw();
square.drawCircle();
Page - 30
square.drawRectangle();
square.drawSquare();
}
Output:-
Page - 31
Practical – 26
(Program in C++ to prepare mark sheet of a university exam by reading student name,
roll no, sub name, subcode, internal marks, external marks. Design a base class
consisting of data members such as student name, roll no, sub name. Derived class
consists of data members such as sub code, internal marks, external marks, construct
oops data to search for a record i.e., be printed)
Description:-
In this program we create a base class that stores student details and a derived class that stores marks details
and inherit the student details from the base class. Then the user can input and display the details.
Source Code:-
#include <iostream>
using namespace std;
class Student {
public:
string name, sub_name, regNo;
};
class Record : public Student{
public:
string sub_code;
int int_marks, ext_marks;
void fetch(){
cout << "Enter the Student Id : ";
cin >> regNo;
cout << "Enter the Student Name : ";
cin >> name;
cout << "Enter the Subject Name : ";
cin >> sub_name;
cout << "Enter the Subject Code : ";
cin >> sub_code;
cout << "Enter the Internal Marks : ";
cin >> int_marks;
cout << "Enter the External Marks : ";
cin >> ext_marks;
}
void display(){
cout << "\nStudent Details : " << endl;
cout << "-----------------------------------" << endl;
cout << "Student Id : " << regNo << endl;
cout << "Student Name : " << name << endl;
Page - 32
cout << "Subject Name : " << sub_name << endl;
cout << "Subject Code : " << sub_code << endl;
cout << "Internal Marks : " << int_marks << endl;
cout << "External Marks : " << ext_marks << endl;
cout << "-----------------------------------" << endl;
}
};
int main(){
Record record;
record.fetch();
record.display();
}
Output:-
Page - 33
Practical – 27
(Program to show static polymorphism)
Description:-
This program demonstrates the concept of static polymorphism. Static polymorphism, also known as
compile-time polymorphism or method overloading, refers to a mechanism in which the compiler selects the
appropriate function or method to call at compile time based on the number, types, and order of arguments
passed to it.
Source Code:-
#include <iostream>
using namespace std;
int main() {
cout<<"The Sum is "<<display(10,20)<<endl;
cout<<"The Sum is "<<display(10,20,30)<<endl;
}
Output:-
Page - 34
Practical – 28
(Program to show dynamic polymorphism)
Description:-
This program demonstrates the concept of dynamic polymorphism. Dynamic polymorphism, also known as
runtime polymorphism, refers to a mechanism where the correct function to be called is determined at
runtime based on the type of the object.
Source Code:-
#include <iostream>
using namespace std;
class Calculate{
public:
int display(int x, int y) {
return x+y;
}
};
int main() {
Subtract s;
cout<<"The Result is "<<s.display(50,20)<<endl;
}
Output:-
Page - 35
Practical – 29
(Write a program to create memory space for a class object using new operator and to
destroy it using delete operator)
Description:-
This program creates memory space using new keyword and destroys the memory space using the delete
keyword.
Source Code:-
#include <iostream>
using namespace std;
int main() {
int *ptr = new int;
*ptr = 42;
cout << "Value After Allocation: " << *ptr << endl;
delete ptr;
cout << "Value After Deallocation: " << *ptr << endl;
}
Output:-
Page - 36
Practical – 30
(Program to open, write and to close file)
Description:-
This program uses the fstream library to create a file, open a file, write into the file and closing the file.
Source Code:-
#include <iostream>
#include<fstream>
using namespace std;
int main(){
ofstream onfile;
onfile.open("myfile.txt");
onfile << "BCA";
cout << "Written in the file";
onfile.close();
}
Output:-
Page - 37
30.2. Writing into a file.
Source Code:-
#include <iostream>
#include<fstream>
using namespace std;
int main(){
ifstream infile;
string str;
infile.open("myfile.txt");
infile >> str;
cout << str;
infile.close();
}
Output:-
Page - 38
Practical – 31
(Program for Exception Handling Divide by zero)
Description:-
This program uses the try and catch blocks to throw and detect and error in the code.
Source Code:-
#include <iostream>
using namespace std;
int main() {
int a, b;
try {
if (b == 0) {
throw "Error: Division by zero!";
}
int result = a / b;
cout << "Result of division: " << result << endl;
} catch (const char* errorMessage) {
cerr << errorMessage << endl;
}
}
Output:-
Page - 39
Practical – 32
(Any other program related to theory program to enhance the understanding of
students in the subject)
Description:-
This program uses the getline function to read data from a file.
Source Code:-
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ifstream file("myfile.txt");
string line;
while (getline(file, line)) {
cout << line << endl;
}
file.close();
}
Output:-
Page - 40