Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 29

Name

Talha Usman
Class
BsCs(2k)
Roll no.
376
Subject
Oop
Submitted to
Haider Ali

Q1:- Write a program to store and print the roll no.,


name, age, address and marks of 15
students using structure
Code
#include <iostream>
#include <string>

using namespace std;


struct Student {
int rollNo;
string name;
int age;
string address;
float marks;
};

int main() {
Student students[15];

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


cout << "Enter details for student " << i + 1 << ":\n";
cout << "Roll No: ";
cin >> students[i].rollNo;

cout << "Name: ";


cin >> students[i].name;

cout << "Age: ";


cin >> students[i].age;

cout << "Address: ";


cin>> students[i].address;
cout << "Marks: ";
cin >> students[i].marks;
}

cout << "\nDetails of all students:\n";


for (int i = 0; i < 15; ++i) {
cout << "Student " << i + 1 << ":\n";
cout << "Roll No: " << students[i].rollNo << endl;
cout << "Name: " << students[i].name << endl;
cout << "Age: " << students[i].age << endl;
cout << "Address: " << students[i].address << endl;
cout << "Marks: " << students[i].marks << endl;
cout << endl;
}

return 0;
}

Output

Q 02:- Write a C++ program to enter the marks of 5 students in


Chemistry, Mathematics and
Physics (each out of 100) using a structure named Marks having
elements roll no., name,
chem_marks, maths_marks and phy_marks and then display the
percentage of each student.

Code:
#include<iostream>

using namespace std;

struct marks {

string name ;

int roll_no ;

int chem_marks;

int phy_marks ;

int math_marks;

};

int main() {

marks k[5];

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

cout << "Enter the data of student " << i+1 << endl;

cout << "Enter The Name: ";

cin.ignore();

getline(cin, k[i].name);

cout << "Enter The Roll Number: ";

cin >> k[i].roll_no;

cout << "Enter the Math Marks: ";


cin >> k[i].math_marks;

cout << "Enter the Chemistry Marks: ";

cin >> k[i].chem_marks;

cout << "Enter the Physics Marks: ";

cin >> k[i].phy_marks;

cout << endl;

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

cout << "The average of student " << k[j].name << " (Roll no. " << k[j].roll_no << ") in all subjects

is: ";

int total = k[j].math_marks + k[j].chem_marks + k[j].phy_marks;

double average = total / 3.0;

cout << average << endl;

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>

using namespace std;

struct Complex {

double real;

double imaginary;

};
int main() {

Complex num1, num2, result;

cout << "Enter real and imaginary parts of first complex number: ";

cin >> num1.real >> num1.imaginary;

cout << "Enter real and imaginary parts of second complex number: ";

cin >> num2.real >> num2.imaginary;

result.real = num1.real + num2.real;

result.imaginary = num1.imaginary + num2.imaginary;

cout << "Addition: " << result.real << " + " << result.imaginary << "i" << endl;

result.real = num1.real - num2.real;

result.imaginary = num1.imaginary - num2.imaginary;

cout << "Subtraction: " << result.real << " + " << result.imaginary << "i" << endl;

result.real = num1.real * num2.real - num1.imaginary * num2.imaginary;

result.imaginary = num1.real * num2.imaginary + num1.imaginary * num2.real;

cout << "Multiplication: " << result.real << " + " << result.imaginary << "i" << endl;

double denominator = num2.real * num2.real + num2.imaginary * num2.imaginary;

result.real = (num1.real * num2.real + num1.imaginary * num2.imaginary) / denominator;


result.imaginary = (num1.imaginary * num2.real - num1.real * num2.imaginary) / denominator;

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>

using namespace std;

class DecimalToBinary {

private:

unsigned long long binaryNumber;


void convertToBinary(unsigned long long decimalNumber) {

binaryNumber = 0;

unsigned long long base = 1;

while (decimalNumber > 0) {

int remainder = decimalNumber % 2;

binaryNumber = binaryNumber + remainder * base;

decimalNumber /= 2;

base = base * 10;

public:

DecimalToBinary(unsigned long long decimalNumber) {

convertToBinary(decimalNumber);

~DecimalToBinary() {

cout << "Binary representation: " << binaryNumber << std::endl;

};

int main() {

unsigned long long decimalNumber;


cout << "Enter a Decimal number: ";

cin >> decimalNumber;

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(int rows) : numRows(rows) {}

~RightAngleTriangle() {
cout << endl;
}

void printPattern() const {


for (int i = 1; i <= numRows; ++i) {
for (int j = 0; j < i; ++j) {
cout << i;
}
cout << endl;
}
}
};

int main() {
int rows;

cout << "Enter number of rows: ";


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

double calculateVolume() const {


return sideLength * sideLength * sideLength;
}
};

int main() {
double side;

cout << "Enter the side length of the cube: ";


cin >> side;

Cube cube(side);

double volume = cube.calculateVolume();

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;

for (int num = 100; num <= 200; ++num) {


if (num % 9 == 0) {
count++;
sum = sum + num;
cout << num << " ";
}
}

cout << endl;


}

int getCount() const {


return count;
}

int getSum() const {


return sum;
}
};

int main() {
DivisibleByNineInRange divisibleByNine;

int count = divisibleByNine.getCount();


int sum = divisibleByNine.getSum();
cout<<endl;
cout << "Count of numbers divisible by 9 between 100 and 200: " << count << endl;
cout<<endl;
cout << "Sum of numbers divisible by 9 between 100 and 200: " << sum << endl;

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

void printSeries() const {


int first = 0, second = 1, next;
std::cout << "Fibonacci Series for " << numTerms << "
terms: ";

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


if (i <= 1)
next = i;
else {
next = first + second;
first = second;
second = next;
}

cout << next << " ";


}

cout << endl;


}
};
int main() {
int numTerms;

cout << "Enter the number of terms for Fibonacci


series: ";
cin >> numTerms;

FibonacciSeries fibSeries(numTerms);

fibSeries.printSeries();

return 0;
}
Output:

Q6:- Write a C++ program of binary to octal conversion


with Constructor.
Code:
#include <iostream>
#include <cmath>
using namespace std;
class BinaryToOctal {
private:
long long binaryNumber;

public:

BinaryToOctal(long long binary) : binaryNumber(binary) {}

long long convertToOctal() {


int octalNumber = 0, decimalNumber = 0, i = 0;

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

Student(const string& studentName) {


name = studentName;
}
string getName() {
return name;
}
};

int main() {

Student student1;
Student student2("ASAD");
cout << "Student 1 name: " << student1.getName() << endl;
cout << "Student 2 name: " << student2.getName() << endl;

return 0;
}

Output:

Q2:- Create a class named 'Rectangle' with two data members-


length and breadth and a
function to calculate the area which is 'length*breadth'. The
class has three constructors which
are :
1 - having no parameter - values of both length and breadth
are assigned zero.
2 - having two numbers as parameters - the two numbers are
assigned as length and breadth
respectively.
3 - having one number as parameter - both length and breadth
are assigned that number.
Now, create objects of the 'Rectangle' class having none, one
and two parameters and print their
areas. (Note: Using Initializer list)
Code:
#include <iostream>
using namespace std;
class Rectangle {
private:
double length;
double breadth;

public:

Rectangle() : length(0), breadth(0) {}


Rectangle(double len, double brd) : length(len), breadth(brd) {}

Rectangle(double side) : length(side), breadth(side) {}


double calculateArea() {
return length * breadth;
}
};

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:

You might also like