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

c++ content

The document contains multiple C++ programming exercises covering various concepts such as classes, inline functions, arrays, static members, abstract classes, operator overloading, recursion, and friend functions. Each exercise includes a program example, its output, and is authored by Devesh Chopkar. The exercises are designed for BCA II students to enhance their understanding of C++ programming fundamentals.

Uploaded by

student1stjust
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)
5 views

c++ content

The document contains multiple C++ programming exercises covering various concepts such as classes, inline functions, arrays, static members, abstract classes, operator overloading, recursion, and friend functions. Each exercise includes a program example, its output, and is authored by Devesh Chopkar. The exercises are designed for BCA II students to enhance their understanding of C++ programming fundamentals.

Uploaded by

student1stjust
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/ 34

Programming in C++ BCA II

1. Write a program using class and object to create a marksheet of student.


Program :

#include <iostream.h>
#include <stdlib.h>

class student {
private:
char name[30];
int rollNo;
float total;
float subject[5];
int i;

public:
void getDetails(void);
void putDetails(void);
};
void student::getDetails(void)
{
cout<< "Enter Name: ";
cin>> name;
cout<< "Enter Roll Number: ";
cin>>rollNo;
total=0;
for (i=0;i<5;i++){
cout<<"\nEnter Marks of Subject"<<i+1<<" : ";
cin>>subject[i];
total += subject[i];
}
void student::putDetails(void)
{

cout<< "Student details: \n";


cout<< "Name: " << name<<"\n Roll Number: " <<rollNo;
cout<< "\nPercentage is : "<< (total*100)/500 ;
}
void main()
{
student pupil;

pupil.getDetails();
pupil.putDetails();

getch();

Devesh Chopkar 1|Page


Programming in C++ BCA II

Output :

Devesh Chopkar 2|Page


Programming in C++ BCA II

2. Write a program using inline function.

Program :
#include <iostream.h>
#include <stdlib.h>

inline void displayNum(int num) {


cout<<num<<endl;
}

void main(){
displayNum(7);
displayNum(5);
displayNum(312);
getch();
}

Output :

Devesh Chopkar 3|Page


Programming in C++ BCA II

3. Write a program to illustrate array within class.


Program :
#include <iostream.h>
#include <stdlib.h>
#include <string.h>

class Employee {
private:
char name[10];
int empId;
double salary[3];
public:
void setInput();
void display();
};
void Employee :: setInput(){
cout << "Enter Employee Name: ";
cin >> name;
cout << "Enter Employee ID: ";
cin >> empId;
for (int i = 0; i < 3; i++) {
cout << "Enter salary for year " << i + 1 << " : ";
cin >> salary[i];
} }
void Employee :: display() {
cout << "\nEmployee Details\n";
cout << "Name: " << name << endl;
cout << "Employee ID: " << empId << endl;
cout << "Salary:\n";
for (int i = 0; i < 3; i++) {
cout << "Year " << i + 1 << ": " << salary[i] << endl;
}
}
void main() {
Employee e;
e.setInput();
e.display();
getch(); }
Output :

Devesh Chopkar 4|Page


Programming in C++ BCA II

4. Write a program to illustrate array of object.


Program :
#include <iostream.h>
#include <stdlib.h>
#include <string.h>
class Student {
private:

int rollNo;
int marks;

public:
void setInput(int r, int m) {
rollNo = r;
marks = m;
}

void display() {
cout << "Roll Number: " << rollNo << endl;
cout << "Marks: " << marks << endl;
}
};

void main() {
Student students[3];

students[0].setInput( 2006, 99);


students[1].setInput( 2007, 96);
students[2].setInput( 2010, 95);
for (int i = 0; i < 3; i++) {
cout << "\nStudent " << i + 1 << " Details:\n";
students[i].display();
}

getch();
}

Devesh Chopkar 5|Page


Programming in C++ BCA II

Output :

Devesh Chopkar 6|Page


Programming in C++ BCA II

5. Write a program to illustrate static member function.


Program :

#include <iostream.h>

#include <stdlib.h>

#include <string.h>

class Student {

public:

static int total;

Student() { total += 1; }

};

int Student::total = 0;

void main()

{ Student s1;

cout<< "Number of students:" << s1.total<<endl;

Student s2;

cout<< "Number of students:" << s2.total<<endl;

Student s3;

cout<< "Number of students:" << s3.total<<endl;

getch(); }

Output :

Devesh Chopkar 7|Page


Programming in C++ BCA II

6. Write a program to illustrate abstract class.


Program :
#include <iostream.h>
#include <stdlib.h>
#include <string.h>
class Shape {
protected:
float dimension;
public:
void getDimension() {
cin>> dimension;
}
virtual float calculateArea() = 0;
};
class Square : public Shape {
public:
float calculateArea() {
return dimension * dimension;
}
};
class Circle : public Shape {
public:
float calculateArea() {
return 3.14 * dimension * dimension;
}
};
void main() {
Square square;
Circle circle;
cout<< "Enter the length of the square: ";
square.getDimension();
cout<< "Area of square: " <<square.calculateArea() <<endl;
cout<< "\nEnter radius of the circle: ";
circle.getDimension();
cout<< "Area of circle: " <<circle.calculateArea() <<endl;
getch();
}
Output :

Devesh Chopkar 8|Page


Programming in C++ BCA II

7. Write a program to illustrate this pointer.


Program :
#include <iostream.h>
#include <stdlib.h>
#include <string.h>
class Product {
private:
int id;
int price;

public:
Product(int i, int r) {
this->id = i;
this->price = r;
}
void display() {
cout << "Product ID : " << this->id << endl;
cout << "Price : " << this->price << endl;
}
};

void main() {
Product P(101,969);
P.display();
getch();
}

Output :

Devesh Chopkar 9|Page


Programming in C++ BCA II

8. Write a program to store information of a student in a structure.


Program :
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
struct student
{
char name[50];
int roll;
float marks;
};
void main()
{
student s;
cout<< "Enter information," <<endl;
cout<< "Enter name: ";
cin>> s.name;
cout<< "Enter roll number: ";
cin>>s.roll;
cout<< "Enter marks: ";
cin>>s.marks;

cout<< "\nDisplaying Information," <<endl;


cout<< "Name: " << s.name <<endl;
cout<< "Roll: " <<s.roll<<endl;
cout<< "Marks: " <<s.marks<<endl;
getch();
}

Output :

Devesh Chopkar 10 | P a g e
Programming in C++ BCA II

9. Write a program to illustrate polymorphism.


Program :
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
float average(float num1, float num2) {
return (num1 + num2)/2;
}
float average(float num1, float num2,float num3) {
return (num1 + num2 + num3)/3;
}
float sum(float num1, float num2, float num3 , float num4) {
return (num1 + num2 + num3 + num4)/4;
}
void main() {
cout<< "average of 3,4 = " <<average(3, 4) <<endl;
cout<< "average of 3,10,9 = " <<average(3, 10,9) <<endl;
cout<< "average of 29,30,110 = " <<average(29, 30, 110) <<endl;

getch();
}

Output :

Devesh Chopkar 11 | P a g e
Programming in C++ BCA II

10. Write a program to illustrate Binary operator overloading.


Program :
#include <iostream.h>
#include <stdlib.h>
#include <string.h>
class Number {
private:
int value;
public:
Number(int val) {
value = val;
}
Number operator+(const Number& n) {
return Number(value + n.value);
}
void display() {
cout << value << endl;
}
};
void main() {
Number n1(5);
Number n2(3);
Number sum = n1 + n2;
cout << "First number: ";
n1.display();
cout << "Second number: ";
n2.display();
cout << "Sum: ";
sum.display();
getch();
}

Output :

Devesh Chopkar 12 | P a g e
Programming in C++ BCA II

11. Write a program to illustrate unary operator overloading.


Program :
#include <iostream.h>
#include <stdlib.h>
#include <string.h>
class Number {
private:
int value;
public:
Number(int val) {
value = val;
}
Number operator-() {
value = -value;
return *this;
}
void display() {
cout << value << endl;
}
};
void main() {
Number n(5);
cout << "Initial value: ";
n.display();
-n;
cout << "After unary minus: ";
n.display();
getch();
}

Output :

Devesh Chopkar 13 | P a g e
Programming in C++ BCA II

12. Write a program using object as a function argument.


Program :
#include <iostream.h>
#include <stdlib.h>
#include <string.h>
class Bill {
public:
int quantity;
int price;
public:
void set(int x,int y) { quantity = x;
price = y ; }
void print() { cout<< "Quantity : " << quantity <<endl;
cout<< "Price of each : " << price <<endl;}
};
Bill sum(Bill ob1,Bill ob2) {
Bill temp;
temp.quantity = ob1.quantity+ ob2.quantity;
temp.price = ob1.price*ob1.quantity + ob2.price*ob2.quantity;
return temp;
}
void main() {
Bill b1,b2;
Bill total;
b1.set(10,30);
b2.set(20,100);
total =sum(b1,b2);
b1.print();
b2.print();
cout<<"Total bill "<<endl;
cout<<"Quantity : "<< total.quantity <<endl;
cout<<"Total Price : "<< total.price <<endl;
getch();
}
Output :

Devesh Chopkar 14 | P a g e
Programming in C++ BCA II

13. Write a program for recursion to Calculate Factorial.


Program :
#include <iostream.h>
#include <stdlib.h>
#include <string.h>
int factorial(int n) {
if (n == 0) {
return 1;
} else {
return n * factorial(n - 1);
}
}

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

int fact = factorial(num);


cout << "Factorial of " << num << " is: " << fact << endl;

getch();
}

Output :

Devesh Chopkar 15 | P a g e
Programming in C++ BCA II

14. Write a program for function overloading.


Program :
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
class Calculator {
public:
int add(int a, int b) {
return a + b;
}

double add(double a, double b) {


return a + b;
}

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


return a + b + c;
}
};

void main() {
Calculator calc;

cout << "Sum of 2 integers: " << calc.add(5, 3) << endl;


cout << "Sum of 2 doubles: " << calc.add(5.5, 3.7) << endl;
cout << "Sum of 3 integers: " << calc.add(5, 3, 2) << endl;

getch();
}
Output :

Devesh Chopkar 16 | P a g e
Programming in C++ BCA II

15. Write a program for friend function to add data object of two different classes.
Program :
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
class ClassA {
private:
int dataA;

public:
ClassA(int value) {
dataA = value;
}
friend int addData(ClassA, ClassB);
};
class ClassB {
private:
int dataB;
public:
ClassB(int value) {
dataB = value;
}
friend int addData(ClassA, ClassB);
};

int addData(ClassA objA, ClassB objB) {


return (objA.dataA + objB.dataB);
}
void main() {
ClassA objA(10);
ClassB objB(20);

int sum = addData(objA, objB);


cout << "Addition of data : " << sum << endl;
getch();
}

Output :

Devesh Chopkar 17 | P a g e
Programming in C++ BCA II

16. Write a program for parameterized constructor.


Program :
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
class Student {
private:
char name[10];
int age;

public:
Student(char n[10], int a) {
name[10] =n[10];
age = a;
}

void display() {
cout << "Name: " << name[10] << endl;
cout << "Age: " << age << endl;
}
};

int main() {

Student s1("Devesh", 20);


Student s2("Alice", 20);

s1.display();
s2.display();

return 0;
}

Output :

Devesh Chopkar 18 | P a g e
Programming in C++ BCA II

17. Write a program for Copy Constructor.


Program :
#include <iostream>
using namespace std;

class Student {
private:
string name;
int age;

public:
Student(string n, int a) {
name = n;
age = a;
}
Student(const Student& s) {
name = s.name;
age = s.age;
}

void display() {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
}
};
int main() {
Student s1("Devesh", 20);
Student s2(s1);
cout << "Student 1:" << endl;
s1.display();
cout << "\nStudent 2 (Copy of Student 1):" << endl;
s2.display();
return 0;
}
Output :

Devesh Chopkar 19 | P a g e
Programming in C++ BCA II

18. Write a program for Destructor.


Program :
#include <iostream>
using namespace std;
class Student {
private:
string name;
int age;
public:
Student(string n, int a) {
name = n;
age = a;
cout << "Constructor called for " << name << endl;
}
~Student() {
cout << "Destructor called for " << name << endl;
}
void display() {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
}
};
int main() {
{
Student s1("Devesh", 20);
s1.display();
}
Student s2("Sam", 19);
s2.display();
return 0;
}

Output :

Devesh Chopkar 20 | P a g e
Programming in C++ BCA II

19. Write a program for single inheritance.


Program :
#include <iostream>
using namespace std;
class Vehicle {
protected:
string name;
public:
Vehicle(string n) {
name = n;
}
void transport() {
cout << name << " is for transport. " << endl;
}
};
class Bike : public Vehicle {
public:
Bike(string n) : Vehicle(n) {}
void ride() {
cout << name << " Bike is Fast. " << endl;
}
};
int main() {
Bike b("Splendor");
b.transport();
b.ride();
return 0;
}

Output :

Devesh Chopkar 21 | P a g e
Programming in C++ BCA II

20. Write a program for multi-level inheritance.


Program :
#include <iostream>
using namespace std;
class Vehicle {
public:
void display() {
cout << "I am a Vehicle." << endl;
}
};
class Car : public Vehicle {
public:
void display() {
Vehicle::display();
cout << "I am a Car." << endl;
}
void accelerate() {
cout << "Car is accelerating." << endl;
}
};
class SportsCar : public Car {
public:
void display() {
Car::display();
cout << "I am a Sports Car." << endl;
}
void turboBoost() {
cout << "Sports Car is turbo boosting." << endl;
}
};
int main() {
SportsCar sc;
sc.display();
sc.accelerate();
sc.turboBoost();
return 0;
}
Output :

Devesh Chopkar 22 | P a g e
Programming in C++ BCA II

21.Write a program for hierarchical inheritance.


Program :
#include <iostream>
using namespace std;
class Animal {
public :
string n ;
public:
void anim() {
cout<< " which comes under Animal." << endl;
} };
class Mammal : public Animal {
public:
string n;
public:
void mam() {
cout<< n << " is a Mammal" ;
}
};
class Bird : public Animal {
public:
string n;
public:
void bir() {
cout<< n << " is a bird" ;
}
};
int main() {
Mammal m;
tiger.n = "Tiger";
tiger.mam();;
tiger.anim();
Bird pigeon;
pigeon.n = "Pigeon";
pigeon.bir();
pigeon.anim();
return 0;
}
Output :

Devesh Chopkar 23 | P a g e
Programming in C++ BCA II

22. Write a program using a pointer to derived class.


Program :
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
class Shape {
public:
void draw() {
cout << "Drawing shape..." << endl;
}
void area() {
cout << "Calculating shape area..." << endl;
}
};
class Circle : public Shape {
public:
void draw(){
cout << "Drawing circle..."<< endl;
}
void area() {
cout << "Calculating circle area..." << endl;
}
};
void main() {
Shape* circlePtr = new Circle();
circlePtr->draw();
circlePtr->area();
delete circlePtr;
getch();
}

Output :

Devesh Chopkar 24 | P a g e
Programming in C++ BCA II

23. Write a program for virtual function.


Program :
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
class Shape {
public:
virtual void draw() {
cout << "Drawing shape..." << endl;
}
virtual void area() {
cout << "Calculating shape area..." << endl;
}
};
class Circle : public Shape {
public:
void draw(){
cout << "Drawing circle..."<< endl;
}
void area() {
cout << "Calculating circle area..." << endl;
}
};
void main() {
Shape* circlePtr = new Circle();
circlePtr->draw();
circlePtr->area();
delete circlePtr;
getch();
}
Output :

Devesh Chopkar 25 | P a g e
Programming in C++ BCA II

24. Write a program for pure virtual function.

Program :
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
class Shape {
public:
virtual void draw() = 0;
virtual void area() = 0;
};

class Circle : public Shape {


private:
int radius;

public:
Circle(int radius) {
this->radius = radius;
}

void draw() {
cout << "Drawing circle..." << endl;
}

void area() {
cout << "Area of circle: " << 3.14 * radius * radius << endl;
}
};

class Rectangle : public Shape {


private:
int length;
int width;

public:
Rectangle(int length, int width) {
this->length = length;
this->width = width;
}

void draw() {
cout << "Drawing rectangle..." << endl;
}

void area() {
cout << "Area of rectangle: " << length * width << endl;
}

Devesh Chopkar 26 | P a g e
Programming in C++ BCA II

};

void main() {
Circle circle(10);
circle.draw();
circle.area();

Rectangle rectangle(5, 10);


rectangle.draw();
rectangle.area();

getch();
}

Output :

Devesh Chopkar 27 | P a g e
Programming in C++ BCA II

25. Write a program using inline function to find minimum of two numbers. The inline function should
take two arguments and should return the minimum value.

Program :
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>

inline int min(int a, int b) {


return (a < b) ? a : b;
}

void main() {
int num1, num2;

cout << "Enter first number: ";


cin >> num1;

cout << "Enter second number: ";


cin >> num2;

cout << "Minimum number: " << min(num1, num2) << endl;

getch();
}

Output :

Devesh Chopkar 28 | P a g e
Programming in C++ BCA II

26. Write swapping program to demonstrate call by value, call by address and call by reference in a
single program.

Program :
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>

void swapByValue(int a, int b) {


int temp = a;
a = b;
b = temp;
cout << "Call by value: a = " << a << ", b = " << b << endl;
}

void swapByAddress(int* a, int* b) {


int temp = *a;
*a = *b;
*b = temp;
cout << "Call by address: a = " << *a << ", b = " << *b << endl;
}

void swapByReference(int& a, int& b) {


int temp = a;
a = b;
b = temp;
cout << "Call by reference: a = " << a << ", b = " << b << endl;
}

void main() {
int a = 5;
int b = 10;

cout << "Original values: a = " << a << ", b = " << b << endl;

swapByValue(a, b);
cout << "After call by value: a = " << a << ", b = " << b << endl;
swapByAddress(&a, &b);
cout << "After call by address: a = " << a << ", b = " << b << endl;
a = 5;
b = 10;
swapByReference(a, b);
cout << "After call by reference: a = " << a << ", b = " << b << endl;

getch();
}

Devesh Chopkar 29 | P a g e
Programming in C++ BCA II

Output :

Devesh Chopkar 30 | P a g e
Programming in C++ BCA II

27. Write program to find biggest number among three numbers using pointer and function.
Program :
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>

int findBiggest(int* num1, int* num2, int* num3) {


if (*num1 > *num2 && *num1 > *num3) {
return *num1;
} else if (*num2 > *num1 && *num2 > *num3) {
return *num2;
} else {
return *num3;
}
}

void main() {
int num1, num2, num3;

cout << "Enter first number: ";


cin >> num1;

cout << "Enter second number: ";


cin >> num2;

cout << "Enter third number: ";


cin >> num3;

int biggest = findBiggest(&num1, &num2, &num3);


cout << "Biggest number: " << biggest << endl;

getch();
}

Output :

Devesh Chopkar 31 | P a g e
Programming in C++ BCA II

28. Write a program to sort the elements using bubble sort.

Program :
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>

void bubbleSort(int arr[], int n) {


for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
void main() {
int arr[] = {604,3,10,5,12,290,110,9,1};
int n = sizeof(arr) / sizeof(arr[0]);

cout << "Original array: ";


printArray(arr, n);

bubbleSort(arr, n);

cout << "Sorted array: ";


printArray(arr, n);

getch();
}

Output :

Devesh Chopkar 32 | P a g e
Programming in C++ BCA II

29. Write a program to search the elements using binary search.

Program :
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
int binarySearch(int arr[], int n, int target) {
int low = 0;
int high = n - 1;

while (low <= high) {


int mid = (low + high) / 2;
if (arr[mid] == target) {
return mid;
} else if (arr[mid] < target) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return -1;
}
void main() {
int arr[] = {3, 5, 12, 19, 21, 23, 40, 56, 72,81, 91};
int n = sizeof(arr) / sizeof(arr[0]);
int target;
cout << "Enter the target element: ";
cin >> target;
int result = binarySearch(arr, n, target);
if (result != -1) {
cout << "Element found at index " << result << endl;
} else {
cout << "Element not found" << endl;
}
getch();
}

Output :

Devesh Chopkar 33 | P a g e
Programming in C++ BCA II

30. Write a class having name Calculate that uses static overloaded function to calculate area of circle,
area of rectangle and area of triangle.
Program :
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
class Calculate {
public:

static double area(double radius) {


return 3.14159 * radius * radius;
}

static double area(double length, double width) {


return length * width;
}

static double area(float base, float height) {


return 0.5 * base * height;
}
};
void main() {
double radius = 5.0;
double length = 4.0;
double width = 3.0;
float base = 3.0;
float height = 4.0;

cout << "Area of circle: " << Calculate::area(radius) << endl;


cout << "Area of rectangle: " << Calculate::area(length, width) << endl;
cout << "Area of triangle: " << Calculate::area(base, height) << endl;
getch();
}

Output :

Devesh Chopkar 34 | P a g e

You might also like