Einführung
In diesem Lab lernst du, wie du das Konzept der Überladung des +-Operators in der C++-Programmiersprache demonstrierst. Die Operatorüberladung ist eine Funktion in C++, die es ermöglicht, einen einzelnen Operator oder Symbol mit unterschiedlichen Bedeutungen zu verwenden, je nachdem, in welchem Kontext er verwendet wird. In diesem Lab zeigen wir, wie zwei Cuboid-Objekte mithilfe des +-Operators addiert werden.
Erstelle eine neue C++-Datei
Erstelle eine neue C++-Datei namens main.cpp im Verzeichnis ~/project.
cd ~/project
touch main.cpp
Schreibe Code, um die Überladung des +-Operators zu demonstrieren
Füge den folgenden Code zu main.cpp hinzu, um eine Klasse namens Cuboid zu erstellen, die einen dreidimensionalen rechteckigen Körper darstellt:
#include <iostream>
using namespace std;
//defining the class Cuboid to demonstrate the concept of Plus Operator Overloading in CPP
class Cuboid {
//Declaring class member variables as public to access from outside the class
public:
double length; // Länge des Cuboids
double breadth; // Breite des Cuboids
double height; // Höhe des Cuboids
public:
double getVolume(void) {
return length * breadth * height;
}
void setLength(double l) {
length = l;
}
void setBreadth(double b) {
breadth = b;
}
void setHeight(double h) {
height = h;
}
// Überlade den +-Operator, um zwei Cuboid-Objekte miteinander zu addieren.
Cuboid operator + (const Cuboid & c) {
Cuboid cuboid;
cuboid.length = this -> length + c.length;
cuboid.breadth = this -> breadth + c.breadth;
cuboid.height = this -> height + c.height;
return cuboid;
}
};
Definiere die Hauptfunktion
Füge den folgenden Code zu main.cpp hinzu, um die main-Funktion zu implementieren, die drei Cuboid-Objekte erstellt, deren Maße setzt, deren Volumina berechnet, zwei der Objekte addiert und die Maße und das Volumen des resultierenden Cuboid-Objekts ausgibt:
//Defining the main method to access the members of the class
int main() {
cout << "\n\nWelcome to LabEx :-)\n\n\n";
cout << " ===== Program to demonstrate the Plus Operator Overloading, in CPP ===== \n\n";
//Declaring the Class objects to access the class members
Cuboid c1;
Cuboid c2;
Cuboid c3;
//To store the volume of the Cuboid
double volume = 0.0;
// Setting the length, breadth and height for the first Cuboid object: c1
c1.setLength(3.0);
c1.setBreadth(4.0);
c1.setHeight(5.0);
// Setting the length, breadth and height for the second Cuboid object: c2
c2.setLength(2.0);
c2.setBreadth(5.0);
c2.setHeight(8.0);
// Finding the Volume of the first Cuboid: c1
cout << "Calling the getVolume() method to find the volume of Cuboid c1\n";
volume = c1.getVolume();
cout << "Volume of the Cuboid c1 is : " << volume << "\n\n\n";
// Finding the Volume of the first Cuboid: c1
cout << "Calling the getVolume() method to find the volume of Cuboid c2\n";
volume = c2.getVolume();
cout << "Volume of the Cuboid c2 is : " << volume << "\n\n\n";
// Adding the two Cuboid objects c1 and c2 to form the third object c3:
c3 = c1 + c2;
// Printing the dimensions of the third Cuboid: c3
cout << "Length of the Cuboid c3 is : " << c3.length << endl;
cout << "Breadth of the Cuboid c3 is : " << c3.breadth << endl;
cout << "Height of the Cuboid c3 is : " << c3.height << endl;
// Finding the Volume of the third Cuboid: c3
cout << "\n\nCalling the getVolume() method to find the volume of Cuboid c3\n";
volume = c3.getVolume();
cout << "Volume of the Cuboid c3 is : " << volume << endl;
cout << "\n\n\n";
return 0;
}
Kompiliere und führe den Code aus
Verwende den folgenden Befehl, um den Code zu kompilieren und auszuführen:
g++ main.cpp -o main && ./main
Du wirst die folgende Ausgabe sehen:
Welcome to LabEx :-)
===== Program to demonstrate the Plus Operator Overloading, in CPP =====
Calling the getVolume() method to find the volume of Cuboid c1
Volume of the Cuboid c1 is : 60
Calling the getVolume() method to find the volume of Cuboid c2
Volume of the Cuboid c2 is : 80
Length of the Cuboid c3 is : 5
Breadth of the Cuboid c3 is : 9
Height of the Cuboid c3 is : 13
Calling the getVolume() method to find the volume of Cuboid c3
Volume of the Cuboid c3 is : 585
Zusammenfassung
In diesem Lab hast du gelernt, wie man das Konzept der Überladung des +-Operators in der C++-Programmiersprache demonstriert. Die Überladung von Operatoren ist eine mächtige und nützliche Eigenschaft von C++, die es dir ermöglicht, einen Operator in verschiedenen Kontexten mit unterschiedlichen Bedeutungen zu verwenden. Indem du den +-Operator überladest, kannst du zwei Cuboid-Objekte miteinander addieren.



