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

Proc++ Divya

Uploaded by

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

Proc++ Divya

Uploaded by

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

VANITA VISHRAM WOMEN’S UNIVERSITY

Managed By Vanita Vishram, Surat


School of Science and Technology
Department Of Computer Science
Program: BCA
2024 - 2025

CERTIFICATE

This is to certify that Miss Pawar Divya of

Class SYBCA, Roll No 2312193 has satisfactorily completed her

Practical \ Laboratory Work for SYBCA Semester 3 in subject of CAM206-2C :

Programming using C++ for the term ending in Month October and year

2024 - 2025.

Date:15 – 08 - 2024

Subject Expert 1 Subject Expert 2

Name: Name:

CHIRAG MEHTA

Signature: Signature:
VANITA VISHRAM WOMEN’S UNIVERSITY
Managed By Vanita Vishram, Surat
School of Science and Technology
Department of Computer Science
Program: BCA

Laboratory \ Practical Index


Sr. No. Particulars Practical Page Signature
Date No
Problem Sheet 15-08-24
1

Problem Sheet
YEAR 2024 – 2025
Sub: - CAM206-2C: Programming using C++

1. Write a program to check given year is leap year or not.


#include <iostream.h>

#include <conio.h>

int isLeapYear(int year) {

if (year % 4 == 0) {

if (year % 100 == 0) {

if (year % 400 == 0) {

return 1; // Leap year

else {

return 0; // Not a leap year

else {

return 1; // Leap year

} else {

return 0; // Not a leap year

void main() {

clrscr();

int year;

// Input year from user

cout << "Enter a year: ";

cin >> year;

// Check if the year is a leap year


1

if (isLeapYear(year)) {

cout << year << " is a leap year." << endl;

} else {

cout << year << " is not a leap year." << endl;

getch();

2. Write a program to generate Electricity bill using class and object.


#include<iostream.h>
#include<conio.h>
class ElectricityBill {

private:
char customerName[50];
int customerID;
int unitsConsumed;
float billAmount;

public:
// Function to input customer details
void inputDetails() {
cout << "Enter Customer Name: ";
cin >> customerName;
cout << "Enter Customer ID: ";
cin >> customerID;
cout << "Enter Units Consumed: ";
cin >> unitsConsumed;
}
// Function to calculate the bill
void calculateBill() {
if (unitsConsumed <= 100) {
1

billAmount = unitsConsumed * 1.5;


}
else if (unitsConsumed <= 200) {
billAmount = 100 * 1.5 + (unitsConsumed - 100) * 2.0;
}
else if (unitsConsumed <= 300) {
billAmount = 100 * 1.5 + 100 * 2.0 + (unitsConsumed - 200) * 3.0;
}
else {
billAmount = 100 * 1.5 + 100 * 2.0 + 100 * 3.0 + (unitsConsumed - 300) * 5.0;
}
}

// Function to display the bill


void displayBill() {
cout << "\n--- Electricity Bill ---\n";
cout << "Customer Name: " << customerName << endl;
cout << "Customer ID: " << customerID << endl;
cout << "Units Consumed: " << unitsConsumed << endl;
cout << "Bill Amount: $" << billAmount << endl;
}
};
int main() {

clrscr();

ElectricityBill bill;
// Input customer details
bill.inputDetails();
// Calculate the bill

bill.calculateBill();
// Display the bill
bill.displayBill();
1

getch();
return 0;
}

3. Write a program to find square root of float and integer using inline function.
#include<iostream.h>
#include<conio.h>
#include<math.h>
// Inline function to calculate square root
inline float calculateSqrt(float num) {

return sqrt(num);
}
int main() {
clrscr();
int intNum;
float floatNum;
// Input integer number
cout << "Enter an integer number: ";
cin >> intNum;
// Input floating-point number
cout << "Enter a floating-point number: ";
cin >> floatNum;

// Calculate and display square roots using the inline function


cout << "\nSquare root of " << intNum << " is: " << calculateSqrt(intNum);
cout << "\nSquare root of " << floatNum << " is: " << calculateSqrt(floatNum);
getch();
return 0;
1

4. Write a program to calculate sum of 5 numbers using Default arguments.


#include<iostream.h>
#include<conio.h>
// Function to calculate sum with default arguments

int sum(int a = 0, int b = 0, int c = 0, int d = 0, int e = 0) {


return a + b + c + d + e;
}
int main() {
clrscr();
int num1, num2, num3, num4, num5;
// Input five numbers
cout << "Enter first number: ";
cin >> num1;
cout << "Enter second number: ";
cin >> num2;
cout << "Enter third number: ";
cin >> num3;
cout << "Enter fourth number: ";
cin >> num4;
cout << "Enter fifth number: ";
cin >> num5;
// Calculate the sum
int total = sum(num1, num2, num3, num4, num5);
// Display the sum
cout << "\nThe sum of the numbers is: " << total;
getch();
1

return 0;
}

5. Write a program for static data member using student class with data members rollNo,
Name and Marks. Count the total number of objects created for same.
#include<iostream.h>
#include<conio.h>
#include<string.h> // Include this header for strcpy function
class Student {
private:
int rollNo;
char Name[50];
float Marks;
public:
static int count; // Static data member to count number of objects
// Constructor to initialize student details
Student(int r, char n[], float m) {

rollNo = r;
strcpy(Name, n); // Copy the string n into Name
Marks = m;

count++; // Increment the object count each time a new object is created
}
// Function to display student details
void display() {
cout << "Roll No: " << rollNo << endl;
cout << "Name: " << Name << endl;
cout << "Marks: " << Marks << endl;
}
1

// Static function to display the total count of objects


static void showCount() {
cout << "\nTotal number of students: " << count << endl;
}
};
// Initialize the static data member
int Student::count = 0;

int main() {
clrscr();
// Create student objects
Student s1(1, "John", 85.5);
Student s2(2, "Alice", 90.0);
Student s3(3, "Bob", 78.5);
// Display details of each student
cout << "Details of Student 1:" << endl;
s1.display();
cout << "\nDetails of Student 2:" << endl;

s2.display();
cout << "\nDetails of Student 3:" << endl;
s3.display();
// Display total number of objects created
Student::showCount();
getch();
return 0;
}
1

6. Write a program for static member function using item class with data members
itemcode, price and quantity and count total number of item purchased.
#include<iostream.h>
#include<conio.h>
class Item {
public:
int itemcode;
float price;
int quantity;

static int count; // Static data member to count total number of items purchased
// Constructor to initialize item details
Item(int code, float pr, int qty) {
itemcode = code;
price = pr;
quantity = qty;

count += quantity; // Add the quantity to the total count


} // Function to display item details
void display() {

cout << "Item Code: " << itemcode << endl;


cout << "Price: $" << price << endl;
cout << "Quantity: " << quantity << endl;
} // Static function to display the total count of items purchased
static void showTotalItems() {
1

cout << "\nTotal number of items purchased: " << count << endl;
}
}; // Initialize the static data member
int Item::count = 0;
int main() {
clrscr(); // Create item objects
Item item1(101, 15.50, 2);
Item item2(102, 25.75, 3);
Item item3(103, 40.00, 5);
// Display details of each item
cout << "Details of Item 1:" << endl;
item1.display();
cout << "\nDetails of Item 2:" << endl;
item2.display();

cout << "\nDetails of Item 3:" << endl;


item3.display();
// Display total number of items purchased
Item :: showTotalItems();
getch();
return 0;

7. Write a program to create class marksheet which display as follow:


#include<iostream.h>
1

#include<conio.h>

class Marksheet {

char name[50];

int rollNumber;

int marks[3]; // 0: English, 1: Maths, 2: Science

public:

void inputData() {

cout << "\nStudent Name : ";

cin >> name;

cout << "\nRoll Number :";

cin >> rollNumber;

cout << "\nMarks of Three Subjects\n";

cout<<" ";

cout << "\nEnglish : ";

cin >> marks[0];

cout << "\nMaths : ";

cin >> marks[1];

cout << "\nScience : ";

cin >> marks[2];

cout<<" ";
}

void displayData() {

cout << name << "\t\t"

<< rollNumber << "\t\t"

<< marks[0] << "\t"

<< marks[1] << "\t"

<< marks[2] << "\n";

};

void main() {

clrscr();

int n;
1

cout << "How Many Students You Want to Enter? : ";

cin >> n;

Marksheet students[10]; // Assuming a maximum of 10 students

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

students[i].inputData();

cout<<" ";

cout<<"\n********** Displaying Student's Details **********\n";

cout<<"\nStudent's Name\tRoll Number\tMarks of Three Subjects\n";

cout<<"\n\t\t\t\tEnglish\tMaths\tScience\n";

for (i = 0; i < n; i++) {

students[i].displayData();

}
getch();

8. Create a class student which have id, name, address details. Use new and delete to create
dynamic object creation.
#include <iostream.h>

#include <conio.h>

#include <string.h> // For strcpy function

class Student {

public:
1

int id;

char name[50];

char address[100];

// Constructor to initialize the Student object

Student(int studentId, const char* studentName, const char* studentAddress) {

id = studentId;

strcpy(name, studentName);

strcpy(address, studentAddress);

// Method to display student details

void display() {

cout << "ID: " << id << endl;

cout << "Name: " << name << endl;

cout << "Address: " << address << endl;

// Destructor

~Student() {

// Destructor logic (if needed)

};

int main() {

clrscr(); // Clear the screen (Turbo C++ specific)

// Create a dynamic object of class Student

Student* studentPtr = new Student(1, "John Doe", "1234 Elm Street");

// Display student details

studentPtr->display();

// Deallocate the dynamic object

delete studentPtr;

getch(); // Wait for user input before closing the console window

return 0;

}
1

9. Write a program of class circle and calculate area of circle using constructor overloading
and destructor.
#include <iostream.h>

#include <conio.h>

class Circle {

public:

float radius; // Default constructor

Circle() {

radius = 0.0;

} // Parameterized constructor

Circle(float r) {

radius = r;
}

// Function to calculate area of the circle

float calculateArea() {

return 3.14159 * radius * radius;

// Destructor

~Circle() {

cout << "Destructor called, freeing Circle resources" << endl;

};

void main() {

clrscr();

// Using default constructor

Circle c1;

cout << "Area of circle with radius " << c1.radius << " is: " << c1.calculateArea() << endl;

// Using parameterized constructor

Circle c2(5.0);
1

cout << "Area of circle with radius " << c2.radius << " is: " << c2.calculateArea() << endl;

getch();

10. Write a program to accept 5 Employee detail which have id, name, salary members.
Display employee detail whose salary is greater than 10000.
#include <iostream.h>

#include <conio.h>

class Employee {

public:

int id;

char name[30];

float salary; // Function to accept employee details

void acceptDetails() {

cout << "Enter Employee ID: ";

cin >> id;

cout << "Enter Employee Name: ";

cin >> name;

cout << "Enter Employee Salary: ";

cin >> salary;

// Function to display employee details

void displayDetails() {

cout << "Employee ID: " << id << endl;

cout << "Employee Name: " << name << endl;

cout << "Employee Salary: " << salary << endl;

};

void main() {

clrscr();

Employee employees[5];
1

// Accept details of 5 employees

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

cout << "Enter details for Employee " << (i + 1) << ":" << endl;

employees[i].acceptDetails();

cout << endl;

// Display employees with salary greater than 10000

cout << "Employees with salary greater than 10000:" << endl;

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

if (employees[i].salary > 10000) {

employees[i].displayDetails();

cout << endl;

}
}

getch();

11. Write a c++ program to create objects of distance class then input two distances in
feet and inches and output the sum of two distances. Make use of Constructors and
Destructors.
#include <iostream.h>

#include <conio.h>
1

class Distance {

public:

int feet;

int inches; // Default constructor

Distance() {

feet = 0;

inches = 0;

} // Parameterized constructor

Distance(int f, int i) {

feet = f;

inches = i;

} // Function to input distance

void inputDistance() {

cout << "Enter feet: ";

cin >> feet;

cout << "Enter inches: ";

cin >> inches; }

// Function to display distance

void displayDistance() {

cout << feet << " feet " << inches << " inches" << endl;

// Function to add two distances

Distance addDistance(Distance d) {

Distance temp;

temp.feet = feet + d.feet;

temp.inches = inches + d.inches;

// Convert inches to feet if >= 12

if (temp.inches >= 12) {

temp.feet += temp.inches / 12;

temp.inches = temp.inches % 12;

}
1

return temp;

// Destructor

~Distance() {

cout << "Destructor called for Distance object with " << feet << " feet " << inches << " inches" << endl;

};

void main() {

clrscr();

// Create two Distance objects

Distance d1, d2;

// Input two distances

cout << "Enter first distance:" << endl;

d1.inputDistance();

cout << "Enter second distance:" << endl;

d2.inputDistance();

// Add the two distances

Distance d3 = d1.addDistance(d2);

// Display the sum

cout << "Sum of distances: ";

d3.displayDistance();

getch();

12. Write a program for class Transport with properties wheels, name, color, type for various

vehicles using constructor overloading.


#include <iostream.h>
1

#include <conio.h>

#include<string.h>

class Transport {

public:

int wheels;

char name[30];

char color[20];

char type[30];

// Default constructor

Transport() {

wheels = 0;

strcpy(name, "Unknown");

strcpy(color, "Unknown");

strcpy(type, "Unknown");

// Parameterized constructor with 2 parameters

Transport(int w, const char* n) {

wheels = w;

strcpy(name, n);

strcpy(color, "Unknown");

strcpy(type, "Unknown");

// Parameterized constructor with 3 parameters

Transport(int w, const char* n, const char* c) {

wheels = w;

strcpy(name, n);

strcpy(color, c);

strcpy(type, "Unknown");

// Parameterized constructor with 4 parameters

Transport(int w, const char* n, const char* c, const char* t) {

wheels = w;
1

strcpy(name, n);

strcpy(color, c);

strcpy(type, t);

// Function to display transport details

void displayDetails() {

cout << "Name: " << name << endl;

cout << "Color: " << color << endl;

cout << "Type: " << type << endl;

cout << "Number of Wheels: " << wheels << endl;

// Destructor

~Transport() {

cout << "Destructor called for " << name << endl;

};

void main() {

clrscr();

// Using default constructor

Transport t1;

cout << "Vehicle 1 Details:" << endl;

t1.displayDetails();

cout << endl;

// Using constructor with 2 parameters

Transport t2(2, "Bicycle");

cout << "Vehicle 2 Details:" << endl;

t2.displayDetails();

cout << endl;

// Using constructor with 3 parameters

Transport t3(4, "Car", "Red");

cout << "Vehicle 3 Details:" << endl;

t3.displayDetails();
1

cout << endl;

// Using constructor with 4 parameters

Transport t4(6, "Truck", "Blue", "Heavy Vehicle");

cout << "Vehicle 4 Details:" << endl;

t4.displayDetails();

cout << endl;

getch();

Inheritance:

13. Create a class mathop1 which have calresult() [which calculate square], create another class

mathop2 which have calresult() [which calculate cube], inherit both classes in calc class.

Create object of calc class and call calresult().

#include <iostream.h>

#include <conio.h>

class MathOp1 {

public:
void calResult(int num) {

int square = num * num;

cout << "Square of " << num << " is: " << square << endl;
1

};

class MathOp2 {

public:

void calResult(int num) {

int cube = num * num * num;

cout << "Cube of " << num << " is: " << cube << endl;

};

// Calc class inheriting MathOp1 and MathOp2

class Calc : public MathOp1, public MathOp2 {

public:

void calSquare(int num) {

MathOp1::calResult(num); // Calls the square function from MathOp1

void calCube(int num) {

MathOp2::calResult(num); // Calls the cube function from MathOp2

};

void main() {

clrscr();

Calc obj;

int num;

cout << "Enter a number: ";

cin >> num;

// Call calResult from MathOp1 to calculate square

obj.calSquare(num);

// Call calResult from MathOp2 to calculate cube

obj.calCube(num);

getch();

}
1

14. Create a base class Vehicle with attributes like color, maxSpeed, and methods like
displayDetails(). Derive classes Car, Truck, and Motorcycle with additional attributes and
methods.
#include <iostream.h>

#include <conio.h>

#include <string.h> // Include this for strcpy()

class Vehicle {

public:
char color[20];

int maxSpeed;

// Constructor to initialize Vehicle

Vehicle(const char* c, int s) {

strcpy(color, c);

maxSpeed = s;

// Method to display vehicle details

void displayDetails() {

cout << "Color: " << color << endl;

cout << "Max Speed: " << maxSpeed << " km/h" << endl;

};

class Car : public Vehicle {

public:

int numberOfDoors;

// Constructor to initialize Car

Car(const char* c, int s, int doors) : Vehicle(c, s) {

numberOfDoors = doors;

// Method to display car details


1

void displayDetails() {

Vehicle::displayDetails(); // Call base class method

cout << "Number of Doors: " << numberOfDoors << endl;

};

class Truck : public Vehicle {

public:

int loadCapacity;

// Constructor to initialize Truck

Truck(const char* c, int s, int capacity) : Vehicle(c, s) {

loadCapacity = capacity;

// Method to display truck details

void displayDetails() {

Vehicle::displayDetails(); // Call base class method

cout << "Load Capacity: " << loadCapacity << " tons" << endl;

};

class Motorcycle : public Vehicle {

public:

int hasSidecar; // Changed from bool to int for Turbo C++ compatibility

// Constructor to initialize Motorcycle

Motorcycle(const char* c, int s, int sidecar) : Vehicle(c, s) {

hasSidecar = sidecar;

// Method to display motorcycle details

void displayDetails() {

Vehicle::displayDetails(); // Call base class method

cout << "Has Sidecar: " << (hasSidecar ? "Yes" : "No") << endl;

};

void main() {
1

clrscr();

// Creating a Car object

Car myCar("Red", 220, 4);

cout << "Car Details:" << endl;

myCar.displayDetails();

cout << endl;

// Creating a Truck object

Truck myTruck("Blue", 120, 15);

cout << "Truck Details:" << endl;

myTruck.displayDetails();

cout << endl;

// Creating a Motorcycle object

Motorcycle myMotorcycle("Black", 180, 1); // Use 1 instead of true for Turbo C++ compatibility

cout << "Motorcycle Details:" << endl;

myMotorcycle.displayDetails();

cout << endl;

getch();

15. Create a base class Person with attributes like name, age, and address. Derive classes
Student, Professor, and Staff with additional attributes and methods.
#include <iostream.h>
#include <conio.h>
#include <string.h> // Include this for strcpy()
class Person {

public:
1

char name[30];
int age;
char address[50];
// Constructor to initialize Person
Person(const char* n, int a, const char* addr) {
strcpy(name, n);
age = a;
strcpy(address, addr);
}
// Method to display person details
void displayDetails() {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;

cout << "Address: " << address << endl;


}
};
class Student : public Person {

public:
int studentID;
char course[30];

// Constructor to initialize Student


Student(const char* n, int a, const char* addr, int id, const char* c)
: Person(n, a, addr) {
studentID = id;
strcpy(course, c);

}
// Method to display student details
void displayDetails() {
Person::displayDetails(); // Call base class method
cout << "Student ID: " << studentID << endl;
1

cout << "Course: " << course << endl;


} };
class Professor : public Person {
public:
int employeeID;
char department[30];
// Constructor to initialize Professor
Professor(const char* n, int a, const char* addr, int id, const char* dept)
: Person(n, a, addr) {
employeeID = id;

strcpy(department, dept);
}
// Method to display professor details
void displayDetails() {
Person::displayDetails(); // Call base class method
cout << "Employee ID: " << employeeID << endl;
cout << "Department: " << department << endl;
}
};
class Staff : public Person {
public:
int staffID;
char role[30];

// Constructor to initialize Staff


Staff(const char* n, int a, const char* addr, int id, const char* r)
: Person(n, a, addr) {
staffID = id;
strcpy(role, r);

}
// Method to display staff details
1

void displayDetails() {
Person::displayDetails(); // Call base class method
cout << "Staff ID: " << staffID << endl;

cout << "Role: " << role << endl;


} };
void main() {
clrscr();
// Creating a Student object
Student student("Alice", 20, "123 Elm Street", 1001, "Computer Science");
cout << "Student Details:" << endl;
student.displayDetails();
cout << endl;
// Creating a Professor object
Professor professor("Dr. Smith", 45, "456 Oak Avenue", 2001, "Mathematics");
cout << "Professor Details:" << endl;

professor.displayDetails();
cout << endl; // Creating a Staff object
Staff staff("John", 35, "789 Pine Road", 3001, "Administrative Assistant");
cout << "Staff Details:" << endl;
staff.displayDetails();
cout << endl;
getch();
}
1

Friend Function:

16. Create a class BankAccount with private attributes account_number and balance.
Implement a friend function display_account_info that can access and print the private
attributes of the BankAccount class.
#include <iostream.h>

#include <conio.h>

class BankAccount {

public:
int account_number;

float balance;

// Constructor to initialize BankAccount

BankAccount(int acc_no, float bal) {

account_number = acc_no;

balance = bal;

// Declare friend function

friend void display_account_info(BankAccount &acc);

};

// Friend function definition


1

void display_account_info(BankAccount &acc) {

cout << "Account Number: " << acc.account_number << endl;

cout << "Balance: " << acc.balance << endl;

void main() {

clrscr();

// Creating a BankAccount object

BankAccount myAccount(123456, 10000.50);

// Displaying account information using friend function

cout << "Bank Account Information:" << endl;

display_account_info(myAccount);

getch();

Polymorphism
17. Create a class geometric which overload area() function.
When given a single value, calculate the area of a square.
For two input returns area assumes the figure is a rectangle.
When a floating-point number is passed to the function, it calculates and returns the
area of a circle.
#include <iostream.h>

#include <conio.h>

class Geometric {

public:
// Function to calculate the area of a square

int area(int side) {

return side * side;

// Function to calculate the area of a rectangle

int area(int length, int width) {


1

return length * width;

// Function to calculate the area of a circle

float area(float radius) {

return 3.14159 * radius * radius;

};

void main() {

clrscr();

Geometric shape;

// Calculate and display the area of a square

int square_side = 5;

cout << "Area of Square with side " << square_side << " is: " << shape.area(square_side) << endl;

// Calculate and display the area of a rectangle

int length = 10, width = 5;

cout << "Area of Rectangle with length " << length << " and width " << width << " is: " << shape.area(length, width)
<< endl;

// Calculate and display the area of a circle

float radius = 7.5;

cout << "Area of Circle with radius " << radius << " is: " << shape.area(radius) << endl;

getch();

18. Write a program to overload increment ++ and decrement -- operator.


#include <iostream.h>

#include <conio.h>

class Counter {

public:
int count;

// Constructor to initialize count

Counter(int c = 0) {
1

count = c;

// Overload the prefix ++ operator

void operator++() {

++count;

// Overload the prefix -- operator

void operator--() {

--count;

// Function to display the current count

void display() {

cout << "Count: " << count << endl;

};

void main() {

clrscr();

Counter c(10); // Initialize counter with value 10

cout << "Initial ";

c.display();

// Using the overloaded ++ operator ++c;

cout << "After incrementing ";

c.display();

// Using the overloaded – operator --c;

cout << "After decrementing ";

c.display();

getch();

You might also like