bigfella
Erfahrenes Mitglied
Hi Leute,
hab n Problem.
Versteh die Klasse. Jedoch möchte ich wissen wie das Überladen funktioniert.
Könnt ihr mir bitte erklären wie die Definition von "box operator+(box b)" abläuft?
Welche Werte werden zur Berechnung zur Ausgabe von "temp.get_area()" verwendet?
Danke!
hab n Problem.
Code:
#include <iostream.h>
#include <conio.h>
class box
{
private:
int length;
int width;
public:
void set(int l, int w);
int get_area(void);
box operator+(box b); // Add two boxes using a method
};
void box::set(int l, int w)
{
length = l;
width = w;
}
int box::get_area(void)
{
return length * width;
}
box box::operator+(box b)
{
box temp;
temp.length = length;
temp.width = width + b.width;
return temp;
}
int main(void)
{
box small, medium, large;
box temp;
small.set(1, 2);
medium.set(3, 4);
large.set(8, 10);
cout << "The area is " << small.get_area() << "\n";
cout << "The area is " << medium.get_area() << "\n";
cout << "The area is " << large.get_area() << "\n";
temp = small + medium;
cout << "The new area is " << temp.get_area() << "\n";
getch();
return 0;
}
Versteh die Klasse. Jedoch möchte ich wissen wie das Überladen funktioniert.
Könnt ihr mir bitte erklären wie die Definition von "box operator+(box b)" abläuft?
Welche Werte werden zur Berechnung zur Ausgabe von "temp.get_area()" verwendet?
Danke!