0% found this document useful (0 votes)
1 views

Lab Manual C++ SCS102

The document is a lab manual for Object Oriented Programming with C++ for BCA-II Semester students at GNA University. It outlines the vision, mission, program educational objectives, outcomes, and specific outcomes, along with a detailed index of practical programming exercises. Each practical includes a description, source code, and expected output for various C++ programming tasks.

Uploaded by

pulkitgoyal1905
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Lab Manual C++ SCS102

The document is a lab manual for Object Oriented Programming with C++ for BCA-II Semester students at GNA University. It outlines the vision, mission, program educational objectives, outcomes, and specific outcomes, along with a detailed index of practical programming exercises. Each practical includes a description, source code, and expected output for various C++ programming tasks.

Uploaded by

pulkitgoyal1905
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 48

LAB MANUAL

Of

Object Oriented Programming with C++ Lab


(SCS102)

BCA-II Sem.

(Even 2024)

School of Computational Science

GNA UNIVERSITY

Sri Hargobindgarh Phagwara- Hoshiarpur Road,


Phagwara, Punjab-144401
Vision of the School:

To develop the skilled computer and IT professionals meeting global


requirements of IT industry.

Mission of the School:

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

PEO1: To produce graduates who will be effective in multidisciplinary and


diverse IT environment.
PEO2: To involve student in lifelong learning to adapt the technological
advancements in the emerging areas of computer applications.
PEO3: To become an entrepreneur who can provide solutions and develop
software products for Enterprise needs.
PEO4: Exhibit the growth of the nation and society by implementing and
acquiring knowledge of upliftment of health, safety, and other societal issues.
Program Outcomes:

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

PO1: Basic knowledge: An ability to apply knowledge of basic mathematics,


science, and domain knowledge to solve the computational problems.

PO2: Problem Solving: Improve reasoning with strong mathematical ability to


identify, formulate and analyze problems related to computer science and exhibit
a sound knowledge of data structure and algorithms.

PO3: Discipline knowledge: An ability to apply discipline specific knowledge to


solve core and/or applied computational problems.

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.

PO5: Ethics: Recognize the social and ethical responsibilities of a professional


working in the discipline.

PO6: Computing Skills: Analyze a problem and identify and define the
computing requirements appropriate to its solution.

PO7: Profession and society: Apply reasoning informed by the contextual


knowledge to assess societal, health, safety, legal and cultural issues and the
consequent responsibilities relevant to the professional practice.

PO8: Environment and sustainability: Understand the impact of professional


solutions in societal and environmental contexts, and demonstrate the knowledge
of, and need for sustainable development.
PO9: Individual and team work: Function effectively as an individual, and as
member or leader in diverse teams, and in multidisciplinary settings.

PO10: Social Contribution: Follow professional software engineering practice


by applying contextual knowledge to assess societal and legal issues.

PO11: Communication: Communicate effectively on complex activities and with


the society at large, such as, being able to comprehend and write effective reports
and design documentation, make effective presentations, and give and receive
clear instructions.

PO12: Project management and finance: Demonstrate knowledge and


understanding of the management principles and apply these to one's own work,
as a member and leader in a team.

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.

PSO2: Successful Career and Entrepreneurship: Explore technical knowledge in


diverse areas of Computer Applications and experience an environment
conducive in cultivating skills for successful career, entrepreneurship and higher
studies.

PSO3: Multidisciplinary Projects: The program prepares the young professional


for a range of computer applications, computer organization, techniques of
computer networking, software engineering-Commerce, Web Designing, Big
Data, Python and Advance JAVA.

PSO4: Problem Solving: Ability to use knowledge gained for solving complex
problems using Computational sciences.
Index

S No Practical Name Page No Date Remarks

1. Program to print “Hello World” in C. 1 11-01-2024

Simple Program on printing “Hello World” and “Hello


2. 2 11-01-2024
Name” where name is the input from the user.
Program to check whether the given number is even or
3. 3 18-01-2024
odd.
Program to check whether the given number is Armstrong
4. 4 18-01-2024
or not.

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
5. 5 25-01-2024
of time (in years) at specific rate of interest. Further
determine whether the bank will benefit by charging simple
interest or compound interest.

Program using functions by passing values call by values


6. 6 25-01-2024
method.
Program using functions by passing call by reference
7. 7 25-01-2024
method.
Program to calculate the factorial of a number using
8. 8 01-02-2024
recursion function.
Program which stores the marks of 5 students in an array
9. and then print all the marks. The marks of students are 75, 9 01-02-2024
85, 80, 95 and 90.
Program which initializes a two-dimensional array and
10. 10 01-02-2024
then print all the elements in a matrix form.
Program to generate all the prime numbers between 1 and
11. 11 08-02-2024
n, where n is a value supplied by the user.
Program to find both the largest and smallest number in a
12. 12 08-02-2024
list of integers.
Program Illustrating Class Declarations, Definition, and
13. 13 – 14 15-02-2024
Accessing Class Members.
Program to illustrate default constructor, parameterized
14. 15 – 17 15-02-2024
constructor and copy constructors.

15. Develop a C++ program to demonstrate the destructors. 18 15-02-2024


Write a program to show the allocation and deallocation of
16. 19 22-02-2024
memory using the new and delete keyword.
Program to create student class, read and print N student’s
17. 20 – 21 22-02-2024
details.
Program to Demonstrate the i) Operator Overloading. ii)
18. 22 – 23 29-02-2024
Function Overloading.

19. Program to Demonstrate Friend Function. 24 29-02-2024

Program to Generate Fibonacci Series use Constructor to


20. 25 14-03-2024
Initialize the Data Members.

21. Program to implement the concept of single inheritance. 26 14-03-2024

22. Program to illustrate the concept of multi-level inheritance. 27 14-03-2024

23. Program to implement the concept of multiple inheritance. 28 21-03-2024

Program to implement the concept of hierarchical


24. 29 21-03-2024
inheritance.

25. Program to implement the concept of hybrid inheritance. 30 – 31 21-03-2024

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
26. consisting of data members such as student name, roll no, 32 – 33 21-03-2024
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.

27. Program to show static polymorphism. 34 21-03-2024

28. Program to show dynamic polymorphism. 35 28-03-2024

Write a program to create memory space for a class object


29. 36 28-03-2024
using new operator and to destroy it using delete operator.

30. Program to open, write and to close file. 37 – 38 04-04-2024

31. Program for Exception Handling Divide by zero. 39 04-04-2024

Any other program related to theory program to enhance


32. 40 04-04-2024
the understanding of students in the subject.
Practical – 01
(Write a program to print “Hello World” in C)

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;

float simple = (principle * rate * time)/100;

cout<<"\nThe Simple Interest is "<<simple;

float total = principle * pow((1 + rate/100) , time);


float compound = total - principle;

cout<<"\n\nThe Compound Interest is "<<compound<<endl;


}

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 change(int num){


return num + 20;
}

int main(){
int num;
cout<<"Enter a number : ";
cin>>num;

cout<<"\nThe Value of number before function call is "<<num;


cout<<"\nValue Changed within function to "<<change(num);
cout<<"\nThe Value of number after function call is "<<num<<"\n\n";
}

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 change(int *p){


*p += 20;
return *p;
}

int main(){
int num;
cout<<"Enter a number : ";
cin>>num;

cout<<"\nThe Value of number before function call is "<<num;


cout<<"\nValue Changed within function to "<<change(&num);
cout<<"\nThe Value of number after function call is "<<num<<"\n\n";
}

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 fact(int f){


if(f==0)
return 1;
return f * fact(f - 1);
}

int main(){
int f;
cout<<"Enter a number : ";
cin>>f;

cout<<"\nThe Factorial of "<<f<<" is "<<fact(f)<<endl;


}

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

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


cout<<"Marks of Student "<<(i+1)<<" is "<<arr[i]<<endl;
}

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;

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


for(int j = 0; j < 3; j++){
cout<<arr[i][j]<<" ";
}
cout<<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;

cout<<endl<<"The prime numbers from 1 to "<<range<<" are ";


for(i = 0; i < range; i++){
for(j = 2; j < i; j++){
if(i % j == 0)
break;
}
if(i==j)
cout<<j<<", ";
}
cout<<endl;
}

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;

cout << "Enter Registration Number : ";


cin >> regNo;

cout << "Enter Marks for Subject 1 : ";


cin >> sub1;

cout << "Enter Marks for Subject 2 : ";


cin >> sub2;

cout << "Enter Marks for Subject 3 : ";


cin >> sub3;
}

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

ladder(const ladder& other) {


length = other.length;
cout<<"Copy Constructor was created. Copied Length : "<<length<<" meters.";
}
};

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;

cout << "Enter Registration Number : ";


cin >> regNo;

cout << "Enter Marks for Subject 1 : ";


cin >> sub1;

cout << "Enter Marks for Subject 2 : ";


cin >> sub2;

cout << "Enter Marks for Subject 3 : ";


cin >> sub3;
}

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 add(int a, int b) {


return a + b;
}

int add(int a, int b, int c) {


return a + b + c;
}

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

void shownum(Number n1) {


cout << "Value of num = " << n1.num << endl;
}

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

class Circle : public Shape {


public:
void drawCircle() {
cout << "Circle is drawn" << endl;
}
};

class Rectangle : public Shape {


public:
void drawRectangle() {
cout << "Rectangle is drawn" << endl;
}
};

class Square : public Circle, public Rectangle {


public:
void drawSquare() {
cout << "Square 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 display(int x, int y) {


return x+y;
}

int display(int x, int y, int z) {


return x+y+z;
}

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

class Subtract : public 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.

30.1. Writing into a 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;

cout << "Enter the numerator: ";


cin >> a;

cout << "Enter the denominator: ";


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

You might also like