C++ Projects
C++ Projects
Talha Usman
Class
BsCs(2k)
Roll no.
376
Subject
Oop
Submitted to
Haider Ali
int main() {
Student students[15];
return 0;
}
Output
Code:
#include<iostream>
struct marks {
string name ;
int roll_no ;
int chem_marks;
int phy_marks ;
int math_marks;
};
int main() {
marks k[5];
cout << "Enter the data of student " << i+1 << endl;
cin.ignore();
getline(cin, k[i].name);
cout << "The average of student " << k[j].name << " (Roll no. " << k[j].roll_no << ") in all subjects
is: ";
return 0;
}
Output:
Q 03:
Declare a structure to represent a complex number (a
number having a real part and
imaginary part). Write C++ functions to add, subtract, multiply
and divide two complex
numbers.
Code:
#include <iostream>
struct Complex {
double real;
double imaginary;
};
int main() {
cout << "Enter real and imaginary parts of first complex number: ";
cout << "Enter real and imaginary parts of second complex number: ";
cout << "Addition: " << result.real << " + " << result.imaginary << "i" << endl;
cout << "Subtraction: " << result.real << " + " << result.imaginary << "i" << endl;
cout << "Multiplication: " << result.real << " + " << result.imaginary << "i" << endl;
cout << "Division: " << result.real << " + " << result.imaginary << "i" << endl;
return 0;
Output:
Q 04:
Write a program in C++ to convert a decimal number
into binary without using an array
and using the constructor and destructor.
Code:
#include <iostream>
class DecimalToBinary {
private:
binaryNumber = 0;
decimalNumber /= 2;
public:
convertToBinary(decimalNumber);
~DecimalToBinary() {
};
int main() {
DecimalToBinary binary(decimalNumber);
return 0;
Output:
Q 05:
Write a program in C++ to print a pattern of right angle
triangle with a number that will
repeat a number in the row by using the constructor and
destructor.
Code
#include <iostream>
using namespace std;
class RightAngleTriangle {
private:
int numRows;
public:
~RightAngleTriangle() {
cout << endl;
}
int main() {
int rows;
RightAngleTriangle triangle(rows);
triangle.printPattern();
return 0;
}
Output:
Q6:- Write a C++ program to calculate the volume of the cube
using constructor.Code:
#include <iostream>
using namespace std;
class Cube {
private:
double sideLength;
public:
Cube(double side) {
sideLength = side;
}
int main() {
double side;
Cube cube(side);
cout << "Volume of the cube with side length " << side << "
units is: " << volume << " cubic units." << std::endl;
return 0;
}
Output:
Q4:- Write a C++ program to find the number and sum
of all integer between 100 and 200
which are divisible by 9 with constructor.
#include <iostream>
using namespace std;
class DivisibleByNineInRange {
private:
int count;
int sum;
public:
DivisibleByNineInRange() {
count = 0;
sum = 0;
int main() {
DivisibleByNineInRange divisibleByNine;
return 0;
}
Q5:- Write a C++ program to print a Fibonacci series
using constructor.
Code:
#include <iostream>
using namespace std;
class FibonacciSeries {
private:
int numTerms;
public:
FibonacciSeries(int n) : numTerms(n) {}
FibonacciSeries fibSeries(numTerms);
fibSeries.printSeries();
return 0;
}
Output:
public:
while (binaryNumber != 0) {
int remainder = binaryNumber % 10;
decimalNumber += remainder * pow(2, i);
++i;
binaryNumber /= 10;
}
i = 1;
while (decimalNumber != 0) {
octalNumber += (decimalNumber % 8) * i;
decimalNumber /= 8;
i *= 10;
}
return octalNumber;
}
};
int main() {
long long binary;
cout << "Enter a binary number: ";
cin >> binary;
BinaryToOctal converter(binary);
cout << "Octal equivalent: " << converter.convertToOctal() <<endl;
return 0;
}
Output:
Constructor Overloading
Q1:- Write a program to print the names of
students by creating a Student class. If no name is
passed while creating an object of Student class,
then the name should be "Unknown", otherwise
the name should be equal to the String value
passed while creating object of Student class.
Code:
#include <iostream>
#include <string>
using namespace std;
class Student {
private:
string name;
public:
Student() {
name = "Unknown";
}
int main() {
Student student1;
Student student2("ASAD");
cout << "Student 1 name: " << student1.getName() << endl;
cout << "Student 2 name: " << student2.getName() << endl;
return 0;
}
Output:
public:
int main() {
Rectangle rect1;
Rectangle rect2(4.5, 5.5);
Rectangle rect3(7.0);
cout << "Area of Rectangle 1: " << rect1.calculateArea() <<endl;
cout << "Area of Rectangle 2: " << rect2.calculateArea() <<endl;
cout << "Area of Rectangle 3: " << rect3.calculateArea() <<endl;
return 0;
}
Output:
Q3:- Suppose you have a Piggie Bank with an initial amount of $50
and you have to add some
more amount to it. Create a class 'AddAmount' with a data member
named 'amount' with an
initial value of $50. Now make two constructors of this class as
follows:
1 - without any parameter - no amount will be added to the Piggie
Bank
2 - having a parameter which is the amount that will be added to the
Piggie Bank
Create an object of the 'AddAmount' class and display the final
amount in the Piggie Bank.
Code:
#include <iostream>
using namespace std;
class AddAmount {
private:
double amount;
public:
AddAmount() : amount(50) {}
AddAmount(double additionalAmount) : amount(50 +
additionalAmount) {}
void displayAmount() {
cout << "Final amount in the piggy bank: $" << amount <<endl;
}
};
int main() {
AddAmount piggyBank1;
AddAmount piggyBank2(200);
piggyBank1.displayAmount();
piggyBank2.displayAmount();
return 0;
}
Output:
Q4:- Write C++ Program to display the cube of the number up to a
given integer using
constructor overloading.
Output
Please enter the number:
3
cube of 1 is: 1
cube of 2 is: 8
cube of 3 is: 27
Code:
#include <iostream>
using namespace std;
class CubeCalculator {
public:
CubeCalculator(int n) {
for (int i = 1; i <= n; ++i) {
cout << "cube of " << i << " is: " << i * i * i <<endl;
}
}
};
int main() {
int givenInteger;
cout << "Please enter the number: ";
cin >> givenInteger;
CubeCalculator calculator(givenInteger);
return 0;
}
Output: