Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
1 views14 pages

Ankit Final File Oops

oops file
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views14 pages

Ankit Final File Oops

oops file
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Practical File

OOPS USING C++ LAB

Submitted in partial fulfilment of the requirement for the award


of the degree
of
Bachelor of Technology
In
COMPUTER SCIENCE AND ENGINEERING
(COMPUTERENGINEERING)

By

ANKIT
(Roll No. 23UGBCE09105)

DR. MUKTA SANDHU MAM


(Skill Assistant Professor, CSE, SFET)

Department of Skill Faculty of Engineering & Technology


Shri Vishwakarma Skill University Dudhola, Palwal
1. Make a Menu Driven Program for Simple Calculator.
1)Add 2)Sub 3)Multiply 4)Division 5)Exit

#include <iostream>
#include<algorithm>
using namespace std;
int main()
{
int choice;
double num1,num2;
do{
cout<<"Menu"<<endl;
cout<<"1. Addition"<<endl;
cout<<"2. Subtraction"<<endl;
cout<<"3. Multiplication"<<endl;
cout<<"4. Division"<<endl;
cout<<"5. Exit"<<endl;
cout<<"Enter You choice: ";
cin>>choice;
if(choice>=1 && choice<=5)
{
cout<<"Enter the value of first Number: ";
cin>>num1;
cout<<"Enter the value of second Number: ";
cin>>num2;
}
switch(choice){
case 1:
cout<<"Result: "<<num1+num2<<endl;
break;
case 2:
cout<<"Result: "<<num1-num2<<endl;
break;
case 3:
cout<<"Result: "<<num1*num2<<endl;
break;
case 4:

cout<<"Result: "<<num1/num2<<endl;
break;
case 5:
cout<<"Exits M to ja rha hu Moj lo!!"<<endl;
break;
default:
cout<<"Your choice is Invalid Please choice the number between (1-
5)!!";
}
} while (choice != 5);
return 0;
}

Output is :-
2. WRITE A C++ CODE FROM MATRIX
MULTIPLICATION AND ADDITION USING CLASS AND
OBJECT.
#include <iostream>
using namespace std;
class Matrix {
private:
int rows, cols;
int** mat;
public:
Matrix(int r, int c) : rows(r), cols(c) {
mat = new int*[rows];
for (int i = 0; i < rows; ++i) {
mat[i] = new int[cols];
}
}
~Matrix() {
for (int i = 0; i < rows; ++i) {
delete[] mat[i];
}
delete[] mat;
}
void input() {
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
cin >> mat[i][j];
}
}
}
void display() const {
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
cout << mat[i][j] << " ";
}
cout << endl;
}
}

Matrix operator+(const Matrix& m) {


if (rows != m.rows || cols != m.cols) {
cout << "Matrix dimensions must match for addition.\n";
exit(1);
}
Matrix result(rows, cols);
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
result.mat[i][j] = mat[i][j] + m.mat[i][j];
}
}
return result;
}
Matrix operator*(const Matrix& m) {
if (cols != m.rows) {
cout << "Matrix dimensions must match for multiplication.\n";
exit(1);
}
Matrix result(rows, m.cols);
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < m.cols; ++j) {
result.mat[i][j] = 0;
for (int k = 0; k < cols; ++k) {
result.mat[i][j] += mat[i][k] * m.mat[k][j];
}
}
}
return result;
}
};
int main() {
int rows1, cols1, rows2, cols2;
cout << "Enter dimensions for Matrix 1 (rows and columns): ";
cin >> rows1 >> cols1;
cout << "Enter dimensions for Matrix 2 (rows and columns): ";
cin >> rows2 >> cols2;
if (rows1 != rows2 || cols1 != cols2) {
cout << "Matrix dimensions must be the same for addition.\n";
}
Matrix m1(rows1, cols1), m2(rows2, cols2);
cout << "Input for Matrix 1:\n";

m1.input();
cout << "Input for Matrix 2:\n";
m2.input();
cout << "Matrix 1: \n";
m1.display();
cout << "Matrix 2: \n";
m2.display();
cout << "Matrix 1 + Matrix 2: \n";
Matrix sum = m1 + m2;
sum.display();
if (cols1 == rows2) {
cout << "Matrix 1 * Matrix 2: \n";
Matrix product = m1 * m2;
product.display();
} else {
cout << "Matrix multiplication is not possible due to incompatible dimensions.\n";
}
return 0;
}
Output is :-
3. WRITE A C++ CODE F MATRIX ADDITION.

ADDITION
#include<iostream>
using namespace std;

int main(){
cout << "This is the matrix addition program " << endl;
int i,j,r,c,mat1[5][5],mat2[5][5], sum[5][5];
cout << "Enter the number of row ";
cin >> r;
cout << "Enter the number of column ";
cin >> c;
cout << "Enter the matrix 1 :";
for (i=0;i<r;i++){
for (j=0;j<c;j++){
cin >> mat1[i][j];
}
}
cout << "Enter the matrix 2 :";
for (i=0;i<r;i++){
for (j=0;j<c;j++){
cin >> mat2[i][j];
}
}

for (i=0;i<r;i++){
for (j=0;j<c;j++){
sum[i][j] = mat1[i][j] + mat2[i][j];
}
}

cout << "The sum of the given matric is :\n";


for (i=0;i<r;i++){
for (j=0;j<c;j++){
cout << sum[i][j] << " " ;
if (j== c-1){
cout << endl;
}
}
}
return 0;
}
OUTPUT IS :-
4.WRITE A C++ CODE F MATRIX MULTIPLICATION
#include<iostream>
using namespace std ;

int main(){
int i,j,k, mat1[5][5],mat2[5][5],mat3[5][5],r,c;
cout << "Enter the number of rows(upto 5 ) ";
cin >> r;
cout << "Enter the number of column (upto 5) ";
cin >> c;
// now take the values of matrices
cout << "Enter the elements of matrix 1 \n";
for (i=0;i<r;i++){
for (j=0;j<c;j++){
cout << "Enter the element "<< i << j <<" ";
cin >> mat1[i][j];
}
}
cout << "Enter the elements of matrix 2 \n ";
for (i=0;i<r;i++){
for (j=0;j<c;j++){
cout << "Enter the element "<< i << j << " ";
cin >> mat2[i][j];
}
}
// Initializing elements of matrix mult to 0.
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{
mat3[i][j]=0;
}
// To multiply the given matrices
for (i=0;i<r;i++){
for (j=0;j<c;j++){
for (k=0;k<r;k++){
mat3[i][j] += mat1[i][k]*mat2[k][j];
}
}
}
// TO print the answer matrix
cout << "The product of these 2 matrices are \n";
for (i=0;i<r;i++){
for (j=0;j<c;j++){
cout << mat3[i][j] << " ";
if (j == c-1 ){
cout << endl;
}
}
}

}
OUTPUT IS :-
5.Write a program using C++ constructor overloading
# include <iostream>
using namespace std;
// defining the class mall in which we will name the stores
class mall{
public:
string coffeestore ;
string clothstore ;
string cinema ;

// defining the default constructor


mall(){
cout << " Default constructor is invoked "<< endl;

}
// defining the parameterized constructor
mall(string cstore , string clstore , string cin ){
coffeestore = cstore;
clothstore = clstore;
cinema = cin;
cout << " Parameterised constructor is invoked " << endl ;
}
// Defining the copy constructure
mall(mall& t ){
coffeestore = t.coffeestore;
clothstore = t.clothstore;
cinema = t.cinema;
cout << " Copy constructor is invoked " << endl ;
}

// Defining the output fuction


public: void display(){
cout << "The name of the coffee store is :" << coffeestore << endl ;
cout << "The name of the cloth store is : " << clothstore << endl;
cout << "The name of the cinema hall is : " << cinema << endl ;
cout << " ";

};

int main(){
// using the default constructor
mall kkmall ;

// now using the parameterised constructor


mall citymall("feecoo" , "wearings" , "watchit" );
citymall.display(); // printing the values
// now calling the copy constructor
mall indiamall(citymall);
indiamall.display(); // printing the value of object
return 0;

Output is :-

You might also like