NOTE : -
THIS CONTENT IS NOT ORIGINAL
17. You can add METHODS To a class or struct
In standard C a struct contains only data. In C++ a struct definition can also include functions. Those functions are owned by the struct and are meant to operate on the data of the struct. Those functions are called METHODS. The example below defines the method surface() on the struct vector:
In the example above, a is an INSTANCE of struct "vector". (Note that the keyword "struct" was not necessary when declaring vector a.)
Just like a function, a method can be an overload of any C++ operator, have any number of parameters (yet one parameter is always implicit: the instance it acts upon), return any type of parameter, or return no parameter at all.
What is a class? It's a struct that keeps its data hidden. Only the methods of the class can access the data. You can't access the data directly, unless authorized by the public: directive. Here is an example of a class definition. It behaves exactly the same way as the struct example above because the class data x and y are defined as public:
In the example above, the main() function changes the data of instance a directly, using a.x = 3 and a.y = 4. This is made possible by the public: directive in the class definition. This is considered bad practice. See chapter 30.
A method is allowed to change the variables of the instance it is acting upon:
In standard C a struct contains only data. In C++ a struct definition can also include functions. Those functions are owned by the struct and are meant to operate on the data of the struct. Those functions are called METHODS. The example below defines the method surface() on the struct vector:
using namespace std;
#include
struct vector
{
double x;
double y;
double surface ()
{
double s;
s = x * y;
if (s < 0) s = -s;
return s;
}
};
int main ()
{
vector a;
a.x = 3;
a.y = 4;
cout << "The surface of a: " << a.surface() << endl;
return 0;
}
| Output |
| The surface of a: 12 |
In the example above, a is an INSTANCE of struct "vector". (Note that the keyword "struct" was not necessary when declaring vector a.)
Just like a function, a method can be an overload of any C++ operator, have any number of parameters (yet one parameter is always implicit: the instance it acts upon), return any type of parameter, or return no parameter at all.
What is a class? It's a struct that keeps its data hidden. Only the methods of the class can access the data. You can't access the data directly, unless authorized by the public: directive. Here is an example of a class definition. It behaves exactly the same way as the struct example above because the class data x and y are defined as public:
using namespace std;
#include
class vector
{
public:
double x;
double y;
double surface ()
{
double s;
s = x * y;
if (s < 0) s = -s;
return s;
}
};
int main ()
{
vector a;
a.x = 3;
a.y = 4;
cout << "The surface of a: " << a.surface() << endl;
return 0;
}
| Output |
| The surface of a: 12 |
In the example above, the main() function changes the data of instance a directly, using a.x = 3 and a.y = 4. This is made possible by the public: directive in the class definition. This is considered bad practice. See chapter 30.
A method is allowed to change the variables of the instance it is acting upon:
using namespace std;
#include
class vector
{
public:
double x;
double y;
vector its_oposite()
{
vector r;
r.x = -x;
r.y = -y;
return r;
}
void be_oposited()
{
x = -x;
y = -y;
}
void be_calculated (double a, double b, double c, double d)
{
x = a - c;
y = b - d;
}
vector operator * (double a)
{
vector r;
r.x = x * a;
r.y = y * a;
return r;
}
};
int main ()
{
vector a, b;
a.x = 3;
a.y = 5;
b = a.its_oposite();
cout << "Vector a: " << a.x << ", " << a.y << endl;
cout << "Vector b: " << b.x << ", " << b.y << endl;
b.be_oposited();
cout << "Vector b: " << b.x << ", " << b.y << endl;
a.be_calculated (7, 8, 3, 2);
cout << "Vector a: " << a.x << ", " << a.y << endl;
a = b * 2;
cout << "Vector a: " << a.x << ", " << a.y << endl;
a = b.its_oposite() * 2;
cout << "Vector a: " << a.x << ", " << a.y << endl;
cout << "x of oposite of a: " << a.its_oposite().x << endl;
return 0;
}
| Output |
| Vector a: 3, 5 Vector b: -3, -5 Vector b: 3, 5 Vector a: 4, 6 Vector a: 6, 10 Vector a: -6, -10 x of oposite of a: 6 |
BIBILOGRAPHY / REFERENCE : - http://www.4p8.com/eric.brasseur/cppcen.html

No comments:
Post a Comment