0% found this document useful (0 votes)
25 views49 pages

C++ Assignment

This document appears to be a student assignment submission for a C++ class. It includes the student's name, college, and other identifying information. The assignment deals with creating C++ classes to represent geometric shapes and calculate properties like area and volume. It includes classes for triangles, boxes, and integers with overloaded operators. The student provides code examples and discussions for each assignment.

Uploaded by

Darkroom
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)
25 views49 pages

C++ Assignment

This document appears to be a student assignment submission for a C++ class. It includes the student's name, college, and other identifying information. The assignment deals with creating C++ classes to represent geometric shapes and calculate properties like area and volume. It includes classes for triangles, boxes, and integers with overloaded operators. The student provides code examples and discussions for each assignment.

Uploaded by

Darkroom
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/ 49

ACHARYA

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

I WOULD LIKE TO EXPRESS MY GRATITUDE TO


“MRS. SOUMI DASGUPTA” FOR HER ABLE
GUIDANCE AND PROVIDING ME ALL THE FACILITY
THAT WAS REQUIRED IN COMPLETING MY PROJECT.

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

INVIGILATOR SUBJECT TR.

ASSIGNMENT NO.- 01

STAEMENT:- To create a class triangle and


including overloading functions for calculating area.

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;

cout<<"area of scalene triangle is:"<<obj.area(4,3,2,1); 4


return 0; }

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

// constructor with arguments


Box(double l, double w, double h) : length(l), width(w), height(h) {}

// function to calculate the surface area of the box


double surfaceArea()
{
return 2 * (length * width + width * height + height * length);
}

// function to calculate the volume of the box


double volume()
{
return length * width * height;
}

// overload the assignment operator


7
Box& operator=(const Box& other)
{
length = other.length;
width = other.width;
height = other.height;

M return *this;
}
};

int main()
{
// create a box using the default constructor
Box b1;

// create a box using the constructor with arguments


Box b2(2, 3, 4);

// assign b2 to b1 using operator overloading


b1 = b2;

// calculate the surface area of b1


std::cout << "Surface area of b1: " << b1.surfaceArea() << std::endl;

// calculate the volume of b1


std::cout << "Volume of b1: " << b1.volume() << std::endl;

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

STATEMENT:- Creating a class person with data


members(name,address,phone number) and
member_functions(get_name(),get_phone_no.(),get_address)
using suitable constructor and destructir. Initialize them
dynamically.

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;

Person() : name(x), address(x), phoneNumber(x) {};


*/
// constructor with arguments
Person(){}

Person(string n, string a, string p)


{
name = new string(n);
address = new string(a);
phoneNumber = new string(p);
}

// destructor
~Person()
{
delete name;
delete address;

delete phoneNumber; 11
}

// member function to get the name


string getName()
{
return *name;
}
// member function to get the address
string getAddress()
{
return *address;
}

// member function to get the phone number


string getPhoneNumber()
{
return *phoneNumber;
}
};

int main()
{
// create a person using the default constructor
Person p1;

// create a person using the constructor with arguments


Person p2("John", "123 Main Street", "123-456-7890");

// get and display the name of p2 12


cout << "Name: " << p2.getName() << endl;

// get and display the address of p2


cout << "Address: " << p2.getAddress() << endl;

// get and display the phone number of p2


cout << "Phone number: " << p2.getPhoneNumber() << endl;
return 0;
}

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

STATEMENT:- Creating a class integer which contains


data members and using constructor and destructor, member
functions and operator overloading(=,++,--).

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

// constructor with argument


Integer(int i) : integer(i) {}

// destructor
~Integer() {}

// member function to get the integer value


int getInteger()
{
return integer;
}

// member function to set the integer value


void setInteger(int i)
{
integer = i;
}

// overload the assignment operator


Integer& operator=(const Integer& other)
{
integer = other.integer;

16
return *this;
}

// overload the increment operator


Integer& operator++()
{
++integer;
return *this;
}

// overload the decrement operator


Integer& operator--()
{
--integer;
return *this;
}
};

int main()
{
// create an integer using the default constructor
Integer i1;

// create an integer using the constructor with argument


Integer i2(42);
// assign i2 to i1

i1 = i2;

17
// assign i2 to i1 using operator overloading
i1 = i2;

// increment i1 using operator overloading


++i1;

// decrement i1 using operator overloading


--i1;

// get and display the value of i1


cout << "Value of i1: " << i1.getInteger() << endl;

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;

cout << "enter matrix a:\n";


m1.get_matrix(2, 2);
cout << "enter matrix b:\n";
m2.get_matrix(2, 2);
cout << "matrix a:" << endl;
m1.put_matrix();
cout << "matrix b:" << endl;
m2.put_matrix();
m3=m1+m2;
matrix *ptr = new matrix_add();
ptr->display();
m3.put_matrix();

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

heap which is referenced by a pointer defined in stack.

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

class ToBinary: public Number {


public:
ToBinary(int v) : Number(v) {}

void display() {
cout << bitset<8>(value) << endl;
}
};

class ToOctal: public Number {


public:
ToOctal(int v) : Number(v) {}

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

STATEMENT:- Designing a class time containing 2


members(hours and minutes) and member functions(to read
time, to display time and summation of 2 times passed as
arguments).

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

Time operator+(const Time& t) {


Time result;
result.hours = hours + t.hours;
result.minutes = minutes + t.minutes;
if (result.minutes >= 60) {
result.hours += result.minutes / 60;
result.minutes = result.minutes % 60;
}
return result;
}
};
int main() {
Time t1, t2;
t1.read();
t2.read();
Time t3 = t1 + t2;

t3.display(); 35
return 0;
}
OUTPUT:-

DISCUSSION:-
Time1= 10hrs 20mins
Time2= 20hrs 30mins
Time3= Time1+time2= 30hrs 50mins.

36
ASSIGNMENT NO.- 09

STATEMENT:- Creating a function swap()


That interchanges the values of 2 arguments sent to it.
Making the function a template function so that it can be used
with any numerical data type.

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>

using namespace std;

template <typename T>


void swap(int &a, int &b) {
int temp = a; 37
a = b;
b = temp;
}
int main() {
cout << "Before swap: x = " << x << ", y = " << y << endl;
swap(x, y);
cout << "After swap: x = " << x << ", y = " << y << endl;
return 0;
}

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>

using namespace std;

const int MAX_SIZE = 10; 39

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

STATEMENT:- Printing a string given by the user in the


reverse order using for loop.

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

cout << str[i];


42
}
cout << endl;
return 0;
}

OUTPUT:-

DISCUSSION:-
A string in c++ is a type of object representing a collection(or sequence) of different characters.

43
ASSIGNMENT NO.- 12

STATEMENT:- Program to print Fibonacci series.

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

You might also like