0% found this document useful (0 votes)
5 views6 pages

Hassan Kazmi

Uploaded by

arslananwer333
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)
5 views6 pages

Hassan Kazmi

Uploaded by

arslananwer333
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/ 6

ASSIGNMENT NO.

04

NAME: M. Hassan kazmi


REG NO: L1S22ASCS0127
SECTION: 22D
SUBJECT: OOP
_______________________________________
Question:
Explain the concept of Class String in detail with proper
definition, Syntax and with Example Program. Also explain
the concept of Operator overloading in C++ with Syntax
with program example in detail
Answer.
Class String:
In C++, the string class is part of the Standard Library and provides a
convenient and powerful way to work with strings. It is defined in the
<string> header file and allows you to create, manipulate, and perform
operations on strings. The string class represents a sequence of
characters and provides a wide range of member functions to handle
various string operations. It abstracts the complexity of working with
strings and provides an interface to perform common string operations
efficiently.
Syntax:
#include <string>
int main() {
string variableName;
variableName = "Hello, World!";
cout<<variableName;
return 0;
}
Example:
#include <iostream>
#include <string>
int main() {
string str1 = "Hello";
string str2 = " World";
string str3;
str3 = str1 + str2;
cout << "Concatenated string: " << str3 << endl;
cout << "Length of str1: " << str1.length() << endl;
cout << "First character of str2: " << str2[0] << endl;
return 0;
}
Operator Overloading:

Operator overloading in C++ allows you to redefine the behavior of


operators when applied to objects of a class. It enables you to use
operators, such as +, -, *, /, ==, !=, etc., with custom objects in a way
that makes sense for the class. By overloading operators, you can
provide intuitive and meaningful operations on objects, enhancing the
expressiveness and usability of your code.

Syntax:
Complex operator+(const Complex& other)
Example:
#include <iostream>
class Complex {
private:
double real;
double imaginary;

public:
Complex(double real = 0.0, double imaginary = 0.0) {
this->real = real;
this->imaginary = imaginary;
}
Complex operator+(const Complex& other) {
Complex result;
result.real = this->real + other.real;
result.imaginary = this->imaginary + other.imaginary;
return result;
}

Complex operator-(const Complex& other) {


Complex result;
result.real = this->real - other.real;
result.imaginary = this->imaginary - other.imaginary;
return result;
}

Complex operator*(const Complex& other) {


Complex result;
result.real = (this->real * other.real) - (this->imaginary *
other.imaginary);
result.imaginary = (this->real * other.imaginary) + (this->imaginary
* other.real);
return result;
}

bool operator==(const Complex& other) {


return (this->real == other.real) && (this->imaginary ==
other.imaginary);
}

void display() {
cout << "Complex number: " << real << " + " << imaginary << "i"
<< endl;
}
};
int main() {
Complex c1(2.0, 3.0);
Complex c2(1.0, 2.0);
Complex sum = c1 + c2;
Complex difference = c1 - c2;
Complex product = c1 * c2;
if (c1 == c2) {
cout << "c1 is equal to c2" << endl;
} else {
cout << "c1 is not equal to c2" <<endl;
}
sum.display();
difference.display();
product.display();
return 0;}

You might also like