C++ Assignment
C++ Assignment
WEST PRAFULLA
CHANDRA
BENGAL COLLEGE
STATE ESTD.1960
UNIVERSITY PRESENTED
BY-
Tanvir sahil
Wbsu (sem-i)
Roll n0.-220445
University regn.-
1012212500192
C++ ASSIGNMENT
SESSION:- 2022-23
C++ ASSIGNMENT
INDEX
SL.NO. TOPIC PAGE.NO DATE SIGNATURE
1 ASSIGNMENT -
01
2 ASSIGNMENT
-03
3 ASSIGNMENT
-03
4 ASSIGNMENT
-04
5 ASSIGNMENT
-05
6 ASSIGNMENT
-06
7 ASSIGNMENT
-07
8 ASSIGNMENT
-08
9 ASSIGNMENT
-09
10 ASSIGNMENT
-10
11 ASSIGNMENT
-11
12 ASSIGNMENT
-12
ACKNOWLEDGEMENT
CERTIFICATE
THIS IS TO CERTIFY THAT I “TANVIR SAHI”
STUDENT OF SEM I (ACHARYA PRAFULLA
CHANDRA COLLEGE), COMPUTER SCIENCE(H) HAS
COMPLETED THE PROJECT ON C++ UNDER THE
GUIDANCE OF “ MRS. SOUMI DASGUPTA”.
M
ASSIGNMENT NO.- 01
ALGORITHM:-
STEP 1: STARt.
M
STEP 2: creating class triangle.
Step 3: defining class.
Step 4: declaring member functions.
Step 5: function declaration and calculating
area.
Step 6: calling THE functions.
Step 7: end.
SOURCE CODE:-
#include<iostream>
3
#include<math.h>
using namespace std;
class triangle
{
public:
// AREA OF RIGHT ANGLED TRIANGLE
int area(int a,int b)
{
return(a*b/2);
}
// AREA OF EQUILATERAL TRIANGLE
float area(float a)
{
return ((a*a)*sqrt(3)/4);
}
// AREA OF ISOSCELES TRIANGLE
int area(int a,int b,int c,int d)
{
return(sqrt(a*(a-b)*(a-c)*(a-d)));
}
};
int main()
{
triangle obj;
cout<<"area of right angled triangle is:"<<obj.area(2,3);
cout<<endl;
cout<<"area of equilateral triangle is:"<<obj.area(4);
cout<<endl;
OUTPUT:-
DISCUSSION:-
Area of right triangle: base*height/2.
Area of equilateral triangle: sqrt ¾ side*side.
Area of isosceles triangle: √s(s-a)(s-b)(s-c).
Where s= semi-primeter
a=b=c=sides
ASSIGNMENT NO- 02 5
STATEMENT:- Creating a class box, initializing its
object using operator overloading and calculating area(),
volume(), of the box using suitable member function.
STEPS:-
STEP 1: STARt.
STEP 2: creating class BOX.
Step 3: defining class.
Step 4: declaring member functions.
Step 5: function declaration and calculating
Area AND VOLUME.
Step 6: calling THE functions.
Step 7: end.
SOURCE CODE:-
#include <iostream> 6
class Box
{
private:
// private data members for the dimensions of the box
double length, width, height;
public:
// default constructor
Box() : length(0), width(0), height(0) {}
M return *this;
}
};
int main()
{
// create a box using the default constructor
Box b1;
return 0;
OUTPUT:- 8
DISCUSSION:-
AREA OF CUBOID: 2*(length*width+width*height+height*length)
VOLUME OF CUBOID:
length*breadth*height
ASSIGNMENT NO.- 03 9
ALGORITHM:-
STEP 1: START
STEP 2: creating class PERSON.
Step 3: defining class.
Step 4: declaring member functions.
Step 5: function declaration and PRINTING
NAME ADDRESS AND PHONE NUMBER
Step 6: calling THE functions.
Step 7: end.
SOURCE CODE:-
#include <iostream>
#include <cstring>
using namespace std;
class Person 10
{
private:
// private data members for the name, address, and phone number
string* name;
string* address;
string* phoneNumber;
public:
// default constructor
/*int x=nullptr;
// destructor
~Person()
{
delete name;
delete address;
delete phoneNumber; 11
}
int main()
{
// create a person using the default constructor
Person p1;
OUTPUT:-
DISCUSSION:-
13
String is a sequence of characters, either as a literal constant or as some kind of variable
A new operator initializes the memory and returns the address of the newly allocated and
initialized memory to the pointer.
Since it’s programmers responsibility to deallocate dynamically allocated memory,
programmers use delete operator in C++ language.
ASSIGNMENT NO.- 04
ALGORITHM:-
STEP 1: START
STEP 2: creating class INTEGER.
Step 3: defining class.
Step 4: declaring member functions.
Step 5: function declaration and OVERLOADING THE
OPERATORS.
Step 6: calling THE functions.
Step 7: end.
SOURCE CODE:-
15
{
private:
// private data member for the integer value
int integer;
public:
// default constructor
Integer() : integer(0) {}
// destructor
~Integer() {}
16
return *this;
}
int main()
{
// create an integer using the default constructor
Integer i1;
i1 = i2;
17
// assign i2 to i1 using operator overloading
i1 = i2;
return 0;
}
18
OUTPUT:-
DISCUSSION:-
Operator overloading is a compile time polymorphism in which the operator is overloaded to provide
the special meaning to the user-defined data type.
19
ASSIGNMENT NO.- 05
STATEMENT:- Creating a class person and
thereby inheriting two classes(student and exam) and
displaying the details of each student.
ALGORITHM:-
STEP 1: START
STEP 2: creating class PERSON.
Step 3: defining class.
Step 4: declaring member functions.
Step 5: function declaration and PRINTING THE DETAILS
OF EACH STUDENT.
Step 6: calling THE functions.
Step 7: end.
SOURCE CODE:-
#include<iostream>
#include<string>
using namespace std;
class person
{
20
protected:
string name;
string gender;
int age;
public:
void read(void)
{
cout<<"enter name:";
cin>>name;
cout<<endl;
cout<<"enter gender:";
cin>>gender;
cout<<endl;
cout<<"enter age:";
cin>>age;
cout<<endl;
}
};
class student
{
protected:
int roll_no,m1,m2,m3;
public:
void readsm()
{
cout<<"enter roll no.:";
cin>>roll_no;
cout<<endl;
cout<<"enter marks 1:";
cin>>m1;
21
cout<<endl;
cout<<"enter marks 2:";
cin>>m2;
cout<<endl;
cout<<"enter marks 3:";
cin>>m3;
cout<<endl;
//=(m1+m2+m3);
// avg=total/3;
}
};
class exam: public person,public student
{
protected:
int total,avg;
public:
void display(void)
{
total=(m1+m2+m3);
avg=total/3;
cout<<"student name is:"<<name<<endl;
cout<<"gender is:"<<gender<<endl;
cout<<"age is:"<<age<<endl;
cout<<"roll no is:"<<roll_no<<endl;;
cout<<"marks for english:"<<m1<<endl;
cout<<"marks for history:"<<m2<<endl;
cout<<"marks for science:"<<m3<<endl;
cout<<"total marks:"<<total<<endl;
cout<<"average marks:"<<avg<<endl;
}
22
}
int main()
{
exam obj;
obj.read();
obj.readsm();
obj.display();
}
OUTPUT:-
DISCUSSION:-
Inheritance is a mechanism of reusing and extending existing classes without modifying them, thus
producing hierarchial relationships between them.
23
ASSIGNMENT NO.- 06
STATEMENT:- Creating a class matrix using dynamic
resource allocation and using suitable member function
to perform addition, subtraction and multiplication
opreration of matrix.
ALGORITHM:-
STEP 1: STARt.
STEP 2: creating class MATRIX.
Step 3: defining class.
Step 4: declaring member functions.
Step 5: function declaration and PERFORMING THE
OPERATIONS ON THE MATRIX.
Step 6: calling THE functions and displaying the results
dynamically.
Step 7: end.
SOURCE CODE:-
#include <iostream>
using namespace std;
class matrix
{
24
public:
int a[2][2];
int row;
int col;
public:
void display()
//displaying the resultant operation
{
cout << "result is:"<<endl;
}
void get_matrix(int r, int c)
//getting rows and columns for the matrix
{
row = r;
col = c;
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
cout << "enter a[" << i << "][" << j << "]" << endl;
cin >> a[i][j];
}
}
}
void put_matrix()
//putting the rows and columns for the matrix
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
25
cout << a[i][j]<<" ";
}
cout << endl;
}
}
//overloading the operator(binary operator overloading)
matrix operator + (const matrix &obj);
matrix operator - (const matrix &obj2);
matrix operator * (const matrix &obj3);
};
//declaring class for matrix addition
class matrix_add : public matrix
{
matrix operator + (const matrix &m2);
};
matrix matrix::operator +(const matrix &m2)
{
matrix m;
m.row = row;
m.col = col;
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
m.a[i][j] = a[i][j] + m2.a[i][j];
}
}
return m;
26
}
//declaring class for matrix subtraction
class matrix_sub : public matrix
{
matrix operator - (const matrix &m3);
};
matrix matrix::operator -(const matrix &m3)
{
matrix m;
m.row = row;
m.col = col;
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
m.a[i][j] = a[i][j] - m3.a[i][j];
}
}
return m;
}
//declaring class for matrix multiplication
class matrix_mul : public matrix
{
matrix operator * (const matrix &m4);
};
matrix matrix::operator *(const matrix &m4)
27
{
matrix m;
m.row = row;
m.col = col;
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
m.a[i][j] = a[i][j] * m4.a[i][j];
}
}
return m;
}
int main()
{
matrix m1,m2,m3;
28
m3=m1-m2;
matrix *ptr1 = new matrix_sub();
ptr->display();
m3.put_matrix();
m3=m1*m2;
matrix *ptr2 = new matrix_mul();
ptr->display();
m3.put_matrix();
return 0;
}
OUTPUT:-
DISCUSSION:-
In dynamic allocation memory is allocated in heap. In this allocation allocated memory is stored in
29
ASSIGNMENT NO.- 07
STATEMENT:- Creating a class number containing a
decimal integer and converting the decimal integer to
binary,octal and hexadecimal integer.
ALGORITHM:-
STEP 1: STARt.
STEP 2: creating class NUMBER.
Step 3: defining class.
Step 4: declaring member functions.
Step 5: function declaration and PERFORMING the
operations.
Step 6: calling THE functions and displaying the results.
Step 7: end.
SOURCE CODE:-
#include <iostream>
#include <bitset>
#include <string>
30
using namespace std;
class Number {
protected:
int value;
public:
Number(int v) : value(v) {}
virtual void display() {
cout << value << endl;
}
};
void display() {
cout << bitset<8>(value) << endl;
}
};
void display() {
cout << oct << value << endl;
}
};
31
class ToHexadecimal: public Number {
public:
ToHexadecimal(int v) : Number(v) {}
void display() {
cout << hex << value << endl;
}
};
int main() {
ToBinary b(255);
b.display();
ToOctal o(255);
o.display();
ToHexadecimal h(255);
h.display();
return 0;
}
OUTPUT:-
32
DISCUSSION:-
DECIMAL TO BINARY- Divide the number by 2 and note the resultant quotient each time.
DECIMAL TO OCTAL- Divide the number by 8 and note the resultant quotient each time.
DECIMAL TO HEXADECIMAL- Divide the number by 16 and note the resultant quotient
each time.
33
ASSIGNMENT NO.- 08
ALGORITHM:-
STEP 1: STARt.
STEP 2: creating class TIME.
Step 3: defining class.
Step 4: declaring member functions.
Step 5: function declaration and PERFORMING the
operations.
Step 6: calling THE functions and displaying the results.
Step 7: end.
SOURCE CODE:-
#include <iostream>
using namespace std;
class Time {
private:
34
int hours;
int minutes;
public:
void read() {
cout << "Enter hours: ";
cin >> hours;
cout << "Enter minutes: ";
cin >> minutes;
}
void display() {
cout << hours << " hours " << minutes << " minutes" << endl;
}
t3.display(); 35
return 0;
}
OUTPUT:-
DISCUSSION:-
Time1= 10hrs 20mins
Time2= 20hrs 30mins
Time3= Time1+time2= 30hrs 50mins.
36
ASSIGNMENT NO.- 09
ALGORITHM:-
STEP 1: STARt.
STEP 2: creating a function swap().
Step 3: interchanging the values of 2 arguments.
Step 4: making the function a template.
Step 5: performing the operatiion.
Step 6: end.
SOURCE CODE:-
#include <iostream>
OUTPUT:-
DISCUSSION:-
The simple idea of template is to pass data type as parameter so that we don’t need to write the same
code for different data types.
ASSIGNMENT NO.- 10 38
STATEMENT:- Creating a class list that will initialize an
integer by taking values from integer displaying the array
elements from in the order as given by the user and also
displaying the elements in reverse order.
ALGORITHM:-
STEP 1: STARt.
STEP 2: creating class LIST.
Step 3: defining class.
Step 4: declaring member functions.
Step 5: function declaration and PERFORMING the
operations.
Step 6: calling THE functions and displaying the results.
Step 7: end.
SOURCE CODE:-
#include <iostream>
class List {
private:
int size;
int data[MAX_SIZE];
public:
List() {
cout << "Enter size of list: ";
cin >> size;
cout << "Enter elements of list: ";
for (int i = 0; i < size; i++) {
cin >> data[i];
}
}
void display() {
cout << "Elements of list: ";
for (int i = 0; i < size; i++) {
cout << data[i] << " ";
}
cout << endl;
}
void reverse_display() {
cout << "Elements of list in reverse order: ";
for (int i = size - 1; i >= 0; i--) {
cout << data[i] << " ";
}
cout << endl;
}
};
40
int main() {
List l;
l.display();
l.reverse_display();
return 0;
}
OUTPUT:-
DISCUSSION:-
Arrays are used to store multiple values in a single variable , instead od declaring separate variables
for each value.
41
ASSIGNMENT NO.- 11
ALGORITHM:-
STEP 1: START.
STEP 2: TAKING INPUT FROM THE USER.
STEP 3: initializing the for loop.
Step 4: performing the operation and printing the result.
Step 5: end.
SOURCE CODE:-
#include <iostream>
#include <string>
using namespace std;
int main() {
string str;
cout << "Enter a string: ";
getline(cin, str);
cout << "Reversed string: ";
for (int i = str.length() - 1; i >= 0; i--) {
OUTPUT:-
DISCUSSION:-
A string in c++ is a type of object representing a collection(or sequence) of different characters.
43
ASSIGNMENT NO.- 12
ALGORITHM:-
Step 1: START.
STEP 2: TAKING INPUT FROM USER.
STEP 3: INITIALIZING THE LOOP.
STEP 4: PRINTING THE RESULT.
STEP 5: END.
SOURCE CODE:-
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the number of terms: ";
cin >> n;
int a = 0, b = 1;
cout << "Fibonacci series: " << a << " " << b << " ";
for (int i = 2; i < n; i++) {
int c = a + b;
cout << c << " ";
a = b;
b = c;
44
}
cout << endl;
return 0;
}
OUTPUT:-
DISCUSSION:-
Fibonacci sequence is executed in the following manner :-
0 1 1 2 3 5 8 13 21……..
Such that every next term is the summation of previous two terms.
45
END