Q1) Write the syntax for function prototype with an ...

28
Object Oriented Programming Using C++ Group C Computer Science Department, CHRIST (Deemed to be University) 1 Q1) Write the syntax for function prototype with an example. (Himanshu Gulechha 1741030) Solution: Syntax: return_type function_name (datatype argument1, datatype argument2,….); example, int addNumbers(int a, int b); is the function prototype which provides following information to the compiler: 1. name of the function is addNumbers() 2. return type of the function is int 3. two arguments of type int are passed to the function Q2) Explain Function Overloading using a class. (Hari Kethan 1741026) Solution: Function overloading is a feature where two or more functions can have the same name but different parameters. Function overloading is usually performed to enhance the readability of the program. If you have to perform one single operation but with different number or type of arguments then we can simply overload the function. Ways to Overload a Function: 1. By changing number of arguments 2. By having different types of arguments Example: Class Diagram: Code: /*function overloading*/ #include<iostream> #include<string.h> using namespace std; class functionoverload { int id; int marks; char name[20];

Transcript of Q1) Write the syntax for function prototype with an ...

Page 1: Q1) Write the syntax for function prototype with an ...

Object Oriented Programming Using C++ Group C

Computer Science Department, CHRIST (Deemed to be University) 1

Q1) Write the syntax for function prototype with an example. (Himanshu Gulechha 1741030)

Solution:

Syntax: return_type function_name (datatype argument1, datatype argument2,….); example, int addNumbers(int a, int b); is the function prototype which provides following information to the compiler:

1. name of the function is addNumbers() 2. return type of the function is int 3. two arguments of type int are passed to the function

Q2) Explain Function Overloading using a class. (Hari Kethan 1741026)

Solution:

Function overloading is a feature where two or more functions can have the same name but different parameters. Function overloading is usually performed to enhance the readability of the program. If you have to perform one single operation but with different number or type of arguments then we can simply overload the function. Ways to Overload a Function: 1. By changing number of arguments 2. By having different types of arguments Example: Class Diagram: Code: /*function overloading*/ #include<iostream> #include<string.h> using namespace std; class functionoverload { int id; int marks; char name[20];

Page 2: Q1) Write the syntax for function prototype with an ...

Object Oriented Programming Using C++ Group C

Computer Science Department, CHRIST (Deemed to be University) 2

int fees; int std; public: int input() { cout<<"enter marks"<<endl; cin>>marks; } int input(int i,int k,int m) { char nam[20]; fees=k; std=m; cout<<"enter the name of the student"<<endl; cin>>nam; strcpy(name,nam); cout<<"id : "<<id<<endl; cout<<"name : "<<name<<endl; cout<<"fees : "<<fees<<endl; cout<<"std : "<<std<<endl; } }; int main() { functionoverload a; a.input(); a.input(1,70000,9); }

Sample I/O: enter marks 100 enter the name of the student thomas id : 1 name : thomas fees : 70000 std : 9

Page 3: Q1) Write the syntax for function prototype with an ...

Object Oriented Programming Using C++ Group C

Computer Science Department, CHRIST (Deemed to be University) 3

Q3) List any five rules for overloading operators. Explain why we need friend functions.

(Harish 1741027)

Solution:

Rules of operator overloading are: I. Only existing operators can be overloaded. New operators cannot be overloaded. II. The overloaded operator must have at least one operand that is of user defined type. III. We cannot change the basic meaning of an operator. That is to say, We cannot redefine the

plus(+) operator to subtract one value from the other. IV. Overloaded operators follow the syntax rules of the original operators. They cannot be

overridden. V. There are some operators that cannot be overloaded like size of operator(sizeof),

membership operator(.), pointer to member operator(.*), scope resolution operator(::), conditional operators(?:) etc

To allow a non-member function to access private members of a class, it needs to be friend of that

class. Friend functions can access private and protected data of the class.

Q4) Compare operator overloading and function overloading with suitable example of same

function. (Harshitha Kapoor 1741028)

Solution:

Function Overloading If any class have multiple functions with same names but different parameters then they are said to be overloaded. Function overloading allows you to use the same name for different functions, to perform, either same or different functions in the same class. Function overloading is usually used to enhance the readability of the program. If you have to perform one single operation but with different number or types of arguments, then you can simply overload the function. Example #include <iostream> using namespace std; /* Number of arguments are different */ void display(char []); // print the string passed as argument void display(char [], char []); int main() { char first[] = "C programming"; char second[] = "C++ programming"; display(first); display(first, second); return 0; } void display(char s[]) { cout << s << endl; } void display(char s[], char t[])

Page 4: Q1) Write the syntax for function prototype with an ...

Object Oriented Programming Using C++ Group C

Computer Science Department, CHRIST (Deemed to be University) 4

{ cout << s << endl << t << endl; } Ouput C programming C programming C++ programming

Operators Overloading Operator overloading is an important concept in C++. It is a type of polymorphism in which an operator is overloaded to give user defined meaning to it. Overloaded operator is used to perform operation on user-defined data type. For example '+' operator can be overloaded to perform addition on various data types, like for Integer, String(concatenation) etc. Types of Operator Urinary operator Binary operator Example #include <iostream> using namespace std; class Complex { private : double real; double imag; public: Complex () {}; Complex (double, double); Complex operator + (Complex); void print(); }; Complex::Complex (double r, double i) { real = r; imag = i; } Complex Complex::operator+ (Complex param) { Complex temp; temp.real = real + param.real; temp.imag = imag + param.imag; return (temp); } void Complex::print() { cout << real << " + i" << imag << endl;

Page 5: Q1) Write the syntax for function prototype with an ...

Object Oriented Programming Using C++ Group C

Computer Science Department, CHRIST (Deemed to be University) 5

} int main () { Complex c1 (3.1, 1.5); Complex c2 (1.2, 2.2); Complex c3; c3 = c1 + c2; //use overloaded + operator c1.print(); c2.print(); c3.print(); return 0; } Output 3.1 + i1.5 1.2 + i2.2 4.3 + i3.7

Q5) Write a program to read a matrix of size m * n from the keyboard and display the same on the

screen. Make the row and column parameters of the matrix as default arguments. Write a main

program that read the values of m and n from the user to test the function and the default values of m

and n are equal to 2. (Krunal 1741032)

Solution:

CLASS DIAGRAM:

PRIVATE SECTION

int mat[10][10], r, c;

PUBLIC SECTION

MATRIX(int m = 2, int n = 2); //PARAMETRIZED CONSTRUCTOR WITH DEFAULT ARGUMENTS void input(); //Function to take input of matrix elements void print(); //Print Matrix

CODE: #include <iostream> using namespace std; class MATRIX { int mat[10][10], r, c; public:

Page 6: Q1) Write the syntax for function prototype with an ...

Object Oriented Programming Using C++ Group C

Computer Science Department, CHRIST (Deemed to be University) 6

MATRIX(int m = 2, int n = 2) //PARAMETRIZED CONSTRUCTOR WITH DEFAULT ARGUMENTS { r = m; c = n; } void input(); //Function to take input of matrix elements void print(); //Print Matrix }; void MATRIX::input() { cout<<"\n\nEnter elements of matrix ("<<r * c<<" elements): "; for(int i = 0; i < r; i++) { for(int j = 0; j < c; j++) { cin>>mat[i][j]; } } } void MATRIX::print() { for(int i = 0; i < r; i++) { for(int j = 0; j < c; j++) { cout<<mat[i][j]<<"\t"; } cout<<"\n\n"; } } int main() { int choice,r,c; while(1) { cout<<"\n\n"; cout<<"\n\nEnter [1] to generate a default MATRIX\n\nEnter [2] to generate your own MATRIX\n\nEnter [3] to EXIT\nEnter choice: "; cin>>choice; switch(choice)

Page 7: Q1) Write the syntax for function prototype with an ...

Object Oriented Programming Using C++ Group C

Computer Science Department, CHRIST (Deemed to be University) 7

{ case 1: { MATRIX M; M.input(); cout<<"\nMATRIX\n"; M.print(); break; } case 2: { cout<<"\n\nEnter order of MATRIX: "; cin>>r>>c; MATRIX M(r,c); M.input(); cout<<"\nMATRIX\n"; M.print(); break; } case 3: return 0; } } } SAMPLE I/O: Enter [1] to generate a default MATRIX Enter [2] to generate your own MATRIX Enter [3] to EXIT 1 Enter elements of MATRIX (4 elements): 1 2 3 4 MATRIX 1 2 3 4 Enter [1] to generate a default MATRIX Enter [2] to generate your own MATRIX Enter [3] to EXIT 2 Enter the order of MATRIX: 3 3 Enter elements of MATRIX (9 elements): 1 2 3 4 5 6 7 8 9 MATRIX 1 2 3 4 5 6 7 8 9

Page 8: Q1) Write the syntax for function prototype with an ...

Object Oriented Programming Using C++ Group C

Computer Science Department, CHRIST (Deemed to be University) 8

Q6) Write a C++ program to implement function overloading for finding the area of the square, area of the cube and area of the circle using a common name. (Himanshu Gulechha 1741030)

Solution:

#include<iostream> using namespace std; void area(int); void area(double); void area(float); main() { int i,s,z; float r; double y; do { cout<<"Choose a shape to find its area\n1.Square\n2.Cube\n3.Circle\n"; cin>>i; switch(i) { case 1: cout<<"Enter the side of the square\n"; cin>>s; area(s); break; case 2: cout<<"Enter the side of cube \n"; cin>>y; area(y); break; case 3: cout<<"Enter the radius of the circle\n"; cin>>r; area(r); break; default:cout<<"Invalid Choice\n"; } cout<<"Do you want to: 1.Continue 2.Exit"<<endl; cin>>z; }while(z!=2); cout<<"Thank You"<<endl; return 0; } void area(int s) { int a=s*s; cout<<"Area of square is "<<a<<endl; } void area(float r) {

Page 9: Q1) Write the syntax for function prototype with an ...

Object Oriented Programming Using C++ Group C

Computer Science Department, CHRIST (Deemed to be University) 9

float a=(22*r*r)/7; cout<<"Area of Circle is "<<a<<endl; } void area(double r) { double a=6*r*r; cout<<"Area of cube is "<<a<<endl; }

Output: Choose a shape to find its area 1.Square 2.Cube 3.Circle 1 Enter the side of the square 6 Area of square is 36 Do you want to: 1.Continue 2.Exit 1 Choose a shape to find its area 1.Square 2.Cube 3.Circle 2 Enter the side of cube 6 Area of cube is 216 Do you want to: 1.Continue 2.Exit 1 Choose a shape to find its area 1.Square 2.Cube 3.Circle 3 Enter the radius of the circle 6 Area of Circle is 113.143 Do you want to: 1.Continue 2.Exit 2 Thank You

Q7) List all the rules of Overloading operators. (Sruthi 1741031)

Solution:

Rules of operator overloading are: 1. Only existing operators can be overloaded. New operators cannot be overloaded. 2. The overloaded operator must have at least one operand that is of user defined type.

Page 10: Q1) Write the syntax for function prototype with an ...

Object Oriented Programming Using C++ Group C

Computer Science Department, CHRIST (Deemed to be University) 10

3. Overloaded operator follows the syntax rules of original operator. 4. Operators can be overloaded but cannot be overridden. 5. All operators are not overloaded, few exemptions exist. 6. We cannot use friend function for few operators. 7. Unary operator: Takes no explicit argument until it is a friend function. 8. Binary operator: Takes one explicit argument and if it is a friend, takes two. 9. For binary operator, left hand operator must be always object and never a value. 10. For binary operator as a friend, left hand operator can be a value. 11. Binary arithmetic operators such as +, -, *, / must explicitly return a value. 12. Binary arithmetic operators must not make attempt to change own arguments.

Q8) Write a C++ program to overload binary "+" operators using friend functions. (Krunal 1741032)

Solution:

CLASS DIAGRAM:

PRIVATE SECTION

int a;

PUBLIC SECTION

DEMO(int x); friend DEMO operator + (DEMO &A, DEMO &B); void print();

CODE: #include <iostream> using namespace std; class DEMO { int a; public: DEMO(){ } DEMO(int x) { a = x; } friend DEMO operator + (DEMO &A, DEMO &B); void print() { cout<<a; } }

Page 11: Q1) Write the syntax for function prototype with an ...

Object Oriented Programming Using C++ Group C

Computer Science Department, CHRIST (Deemed to be University) 11

DEMO operator + (DEMO &A, DEMO &B) { DEMO C; C.a = A.a + B.a; return C; } int main() { int m,n; cout<<"\n\nEnter any two numbers: "; cin>>m>>n; DEMO A(m),B(n), C; C = A + B; cout<<"\n\nThe sum of the two numbers is: "; C.print(); return 0; } SAMPLE I/O:

Enter any two number: 2 4 Sum of the two numbers is 6

Q9) Write a function power() to raise a number m to a power n. The function takes a double value for

m and int value for n, and returns the result correctly. Use a default value of 3 for n to make the

function to calculate cube when this argument is omitted. Write a main function that gets the values

of m and n from the user to test the function. (Madhumitha 1741033)

Solution:

CODE: /* Program to implement Power function.*/ #include<iostream> using namespace std; double power (double a, int b=3); main() { int a, c; double b,z; cout<<”Enter your choice \n1.Find cube \n2.Find other power\n”; cin>>a; switch(a) { case 1: cout<<”Enter a number\n”;

Page 12: Q1) Write the syntax for function prototype with an ...

Object Oriented Programming Using C++ Group C

Computer Science Department, CHRIST (Deemed to be University) 12

cin>>b; z=power(b); break; case 2: cout<<”Enter a number\n”; cin>>b; cout<<”Enter power\n”; cin>>c; z=power(b,c); default: cout<<”Invalid choice.”; } cout<<”The answer=”<<z<<endl; return 0; } double power(double a, int b) { double z; for(int i=0;i<=b;i++) z=z*a; return z; }

OUTPUT:

Enter your choice: 1.Find cube 2.Find other power 1 Enter a number: 3 The answer is 9

Q10) Write a c++ program to implement the concept of function overloading for the concept absolute

(hint: for int, float and long int). (Gauri 1741025)

Solution:

CODE: #include <iostream> using namespace std; /* Function arguments are of different data type */ long add(long, long); float add(float, float); int add (int); int main() { long a, b, x; float c, d, y;

Page 13: Q1) Write the syntax for function prototype with an ...

Object Oriented Programming Using C++ Group C

Computer Science Department, CHRIST (Deemed to be University) 13

int z,e; cout<< "enter integer to be doubled"; cin>>e; z=add(e); cout << "double of an integer is: " << z << endl; cout << "Enter two integers\n"; cin >> a >> b; x = add(a, b); cout << "Sum of integers: " << x << endl; cout << "Enter two floating point numbers\n"; cin >> c >> d; y = add(c, d); cout << "Sum of floats: " << y << endl; return 0; } long add(long x, long y) { cout<<"long addition called\n"; long sum; sum = x + y; return sum; } float add(float x, float y) { cout<<"float addition called\n"; float sum; sum = x + y; return sum; } int add(int x) { int sum; cout<<"integer addition called\n"; sum = x + x; return sum;

Page 14: Q1) Write the syntax for function prototype with an ...

Object Oriented Programming Using C++ Group C

Computer Science Department, CHRIST (Deemed to be University) 14

}

OUTPUT: enter integer to be doubled 44 integer addition called double of an integer is: 88 Enter two integers 23456 12345 long addition called Sum of integers: 35801 Enter two floating point numbers 1.23 234.32 float addition called Sum of floats: 235.55

Q11) Explain post increment and pre increment operator with the help of a C++ program using

member functions. (Manuj 1741034)

Solution:

Class Diagram:

INCREEMENT

CODE: #include<iostream> using namespace std; class increment { Private: int a; public: int operator ++() //PRE-INCREMENT OPERATOR { int bb; bb=++a;

Page 15: Q1) Write the syntax for function prototype with an ...

Object Oriented Programming Using C++ Group C

Computer Science Department, CHRIST (Deemed to be University) 15

return bb; } int operator ++(int) //POST-INCREMENT OPERATOR { int aa; aa=a++; return aa; } void setvalue() { a=10; } Void display(int a) { Cout<<a<<endl; } }; main() { increment t1; t1.setvalue(); int b=t1++; cout<<b<<" \n"; t1.display(a); int c=++t1; cout<<c<<" \n";; t1.display(a); } Sample I/O:

10 11 12 12

Page 16: Q1) Write the syntax for function prototype with an ...

Object Oriented Programming Using C++ Group C

Computer Science Department, CHRIST (Deemed to be University) 16

Q12) Create a class FLOAT that contains one float data member. Overload all the four arithmetic

operators so that they operate on the objects of FLOAT. (Harish 1741027)

Solution:

Class Diagram:

CODE: #include<iostream> using namespace std; class FLOAT { public: float n; friend FLOAT operator +( FLOAT&, FLOAT &); friend FLOAT operator -(FLOAT &, FLOAT &); friend FLOAT operator *(FLOAT &, FLOAT &); friend FLOAT operator /(FLOAT &, FLOAT &); }; FLOAT operator +(FLOAT &a,FLOAT &b) { FLOAT c; c.n=a.n+b.n; return c; } FLOAT operator -(FLOAT &a,FLOAT &b) { FLOAT c; c.n=a.n-b.n; return c; } FLOAT operator *(FLOAT &a,FLOAT &b) { FLOAT c;

Page 17: Q1) Write the syntax for function prototype with an ...

Object Oriented Programming Using C++ Group C

Computer Science Department, CHRIST (Deemed to be University) 17

c.n=a.n*b.n; return c; } FLOAT operator /(FLOAT &a,FLOAT &b) { FLOAT c; c.n=a.n /b.n; return c; } main() { int a; FLOAT a1,a2,a3; cout<<"Enter a number"<<endl; cin>>a1.n; cout<<"Enter another number"<<endl; cin>>a2.n; do { cout<<"\n\nChoose the operation"<<endl; cout<<"\n1.Add\n2.Subtract\n3.Multiply\n4.Divide\n5.End"<<endl; cin>>a; switch(a) { case 1: a3=a1+a2; cout<<a3.n; break; case 2: a3=a1-a2; cout<<a3.n; break; case 3: a3=a1*a2; cout<<a3.n; break; case 4: a3=a1/a2; cout<<a3.n; break; case 5: break; default:cout<<"Invalid Choice"<<endl; break; } }while(a!=5); cout<<"Thank You"; return 0; }

Page 18: Q1) Write the syntax for function prototype with an ...

Object Oriented Programming Using C++ Group C

Computer Science Department, CHRIST (Deemed to be University) 18

Sample I/O: Enter a number 10 Enter another number 5 Choose the operation 1.Add 2.Subtract 3.Multiply 4.Divide 5.End 1 15 Choose the operation 1.Add 2.Subtract 3.Multiply 4.Divide 5.End 2 5 Choose the operation 1.Add 2.Subtract 3.Multiply 4.Divide 5.End 3 50 Choose the operation 1.Add 2.Subtract 3.Multiply 4.Divide 5.End 4 2 Choose the operation 1.Add 2.Subtract 3.Multiply 4.Divide 5.End 5 Thank You

Page 19: Q1) Write the syntax for function prototype with an ...

Object Oriented Programming Using C++ Group C

Computer Science Department, CHRIST (Deemed to be University) 19

Q13) What is operator overloading? (Hari Kethan 1741026)

Solution:

Operator overloading is an important concept in c++. It is a type of polymorphism in which an operator is overloaded to give user defined meaning to it. Overload operator is used to perform operation on user-defined data type. For example ‘+’ operator can be overloaded to perform addition on various datatypes But there are some operators that can’t be overloaded they are: 1. Scope operator - :: 2. Sizeof 3. Member selector - . 4. Member pointer selector - * 5. Ternary operator - ?:

Q14) Develop a class String. Use overloaded <= , >= and != operator to compare two Strings.

(Harshitha Kapoor 1741028)

Solution:

CLASS DIAGRAM:

Code:

#include<iostream> #include<stdio.h> #include<string.h> #include<conio.h> using namespace std; class strclass { public: char s1[20]; void getdata() { gets(s1); } void showdata() { cout<<s1;

Public:char s1[20];

void getdata(); void showdata();

int operator >=(strclass obj); int operator <=(strclass obj); int operator !=(strclass obj);

Page 20: Q1) Write the syntax for function prototype with an ...

Object Oriented Programming Using C++ Group C

Computer Science Department, CHRIST (Deemed to be University) 20

} int operator>=(strclass obj) { int temp,temp1; temp=strlen(s1); temp1=strlen(obj.s1); if(temp>=temp1) { return 1; } else return 0; } int operator<=(strclass obj) { int temp,temp1; temp=strlen(s1); temp1=strlen(obj.s1); if(temp<=temp1) { return 1; } else return 0; } int operator!=(strclass obj) { int temp,temp1; temp=strlen(s1); temp1=strlen(obj.s1); if(temp!=temp1) { return 1; } else { return 0; } } };

Page 21: Q1) Write the syntax for function prototype with an ...

Object Oriented Programming Using C++ Group C

Computer Science Department, CHRIST (Deemed to be University) 21

main() { strclass obj1,obj2; int temp3,ch,h; cout<<"\nEnter string 1:"; fflush(stdin); obj1.getdata(); cout<<"\nEnter string 2:"; fflush(stdin); obj2.getdata(); do { cout<<"\n1.Use of <= operator"; cout<<"\n2.Use of >= operator"; cout<<"\n3.Use of != operator"; cout<<"\nENter your choice:"; cin>>ch; if(ch==1) { if(obj1>=obj2) { cout<<"\nString 1 is greater"; } else { cout<<"\nString 2 is greater"; } } else if(ch==2) { if(obj1<=obj2) { cout<<"\nString 1 is lesser"; } else { cout<<"\nString 2 is lesser"; } } else { if(obj1!=obj2) { cout<<"\nStrings are not equal";

Page 22: Q1) Write the syntax for function prototype with an ...

Object Oriented Programming Using C++ Group C

Computer Science Department, CHRIST (Deemed to be University) 22

} else { cout<<"\nStrings are equal"; } } cout<<"\nDo you want to continue?(1)"; cin>>h; }while(h==1); getch(); }

OUTPUT: Enter string 1:computer Enter string 2:programming 1.Use of <= operator 2.Use of >= operator 3.Use of != operator ENter your choice:1 String 2 is greater Do you want to continue?(1)1 1.Use of <= operator 2.Use of >= operator 3.Use of != operator ENter your choice:2 String 1 is lesser Do you want to continue?(1)1 1.Use of <= operator 2.Use of >= operator 3.Use of != operator ENter your choice:3 Strings are not equal Do you want to continue?(1)

Q15) List the operators where a friend function cannot be used in overloading.

(Harshitha Srinivas 1741029)

Solution:

The operators that cannot be overloaded by friend function are as follows: Assignment operator = function call operator ()

Page 23: Q1) Write the syntax for function prototype with an ...

Object Oriented Programming Using C++ Group C

Computer Science Department, CHRIST (Deemed to be University) 23

subscript operator [] class member access operator ->

Q16) Create a class MATRIX of size m * n. Perform operator function of the following operation on

two matrices: i) addition ii) multiplication (Harshitha Srinivas 1741029)

Solution:

CLASS DIAGRAM: MATRIX

CODE: #include<iostream> #define SIZE 5 using namespace std; class matrix { public: int row; int column; float array[SIZE][SIZE]; friend istream &operator >>(istream &, matrix &); friend ostream &operator << (ostream&,matrix &); friend matrix operator +(matrix &,matrix &); friend matrix operator *(matrix &,matrix &); friend int operator ==(matrix&,matrix &); }; istream &operator >>(istream &din,matrix &b) { cout<<"Enter the number of rows"<<endl; cin>>b.row; cout<<"Enter the number of columns"<<endl; cin>>b.column; cout<<"Enter the elements"<<endl; for(int i=0;i<b.row;i++) {

Page 24: Q1) Write the syntax for function prototype with an ...

Object Oriented Programming Using C++ Group C

Computer Science Department, CHRIST (Deemed to be University) 24

for(int j=0;j<b.column;j++) { din>>b.array[i][j]; } } } ostream &operator << (ostream & dout,matrix &b) { cout<<"The array is:"<<endl; for(int i=0;i<b.row;i++) { for(int j=0;j<b.column;j++) { dout<<b.array[i][j]<<"\t"; } cout<<endl; } } matrix operator +(matrix &a,matrix &b) { matrix c; c.row=a.row; c.column=a.column; for(int i=0;i<c.row;i++) { for(int j=0;j<c.column;j++) { c.array[i][j]=a.array[i][j]+b.array[i][j]; } } return c; } matrix operator *(matrix &a,matrix &b) { matrix c; c.row=a.row; c.column=a.column; for(int i=0;i<c.row;i++) { for(int j=0;j<c.column;j++) { c.array[i][j]=a.array[i][j]*b.array[i][j]; } } return c; }

Page 25: Q1) Write the syntax for function prototype with an ...

Object Oriented Programming Using C++ Group C

Computer Science Department, CHRIST (Deemed to be University) 25

int operator ==(matrix&a,matrix&b) { if(a.row==b.row && a.column== b.column) return 1; else return 0; } main() { int a; matrix a1,a2,a3; do { cout<<"Choose the operation"<<endl; cout<<"1.Store Matrices\n2.Add Matrices\n3.Multiply Matrices\n4.End"<<endl; cin>>a; switch(a) { case 1: cin>>a1; cout<<a1; cin>>a2; cout<<a2; break; case 2: if(a1==a2) { a3=a1+a2; cout<<a3; } else cout<<"Matrix's Rows and columns do not match"<<endl; break; case 3: if(a1==a2) { a3=a1*a2; cout<<a3; } else cout<<"Matrix's Rows and columns do not match"<<endl; break; case 4: break; default:cout<<"Invalid Choice"<<endl; break; }

Page 26: Q1) Write the syntax for function prototype with an ...

Object Oriented Programming Using C++ Group C

Computer Science Department, CHRIST (Deemed to be University) 26

}while(a!=4); cout<<"Thank You"; return 0; }

SAMPLE I/O:

Enter the number of rows and columns: 2 2 Enter the elements of the first matrix: 9 8 7 6 Enter the elements of the second matrix: 1 2 3 4 Sum of entered Matrix: 10 10 10 10 Multiplication of entered Matrix: 9 16 21 24

Q17) Write a program to add two complex numbers using friend function using binary operator

overloading. (Sruthi 1741031)

Solution:

CLASS DIAGRAM Complex

Private:

int num1, num2

Public:

Complex operator +(Complex c1, Complex c2)

void accept ()

void display ()

friend Complex operator +(Complex c1, Complex c2)

Page 27: Q1) Write the syntax for function prototype with an ...

Object Oriented Programming Using C++ Group C

Computer Science Department, CHRIST (Deemed to be University) 27

CODE: #include<iostream> using namespace std; class Complex { int num1, num2; public: void accept () { cout<<"\n Enter Two Complex Numbers : "; cin>>num1>>num2; } //Overloading '+' operator using Friend function friend Complex operator+(Complex c1, Complex c2); void display() { cout<<num1<<"+"<<num2<<"i"<<"\n"; } }; Complex operator+(Complex c1, Complex c2) { Complex c; c.num1=c1.num1+c2.num1; c.num2=c1.num2+c2.num2; return(c); } int main () { Complex c1, c2, sum; //Created Object of Class Complex i.e c1 and c2 c1.accept(); //Accepting the values c2.accept(); sum = c1+c2; //Addition of object cout<<"\n Entered Values : \n"; cout<<"\t"; c1.display(); //Displaying user input values cout<<"\t"; c2.display(); cout<<"\n Addition of Real and Imaginary Numbers : \n"; cout<<"\t"; sum.display(); //Displaying the addition of real and imaginary numbers

Page 28: Q1) Write the syntax for function prototype with an ...

Object Oriented Programming Using C++ Group C

Computer Science Department, CHRIST (Deemed to be University) 28

return 0; } Sample I/O:

Enter Two Complex Numbers : 5 6 Enter Two Complex Numbers : 7 8 Entered Values : 5 + 6i 7 + 8i Addition of Real and Imaginary Numbers : 12 + 14i