C++ classes members include variables
(including other structures and classes),
functions (specific identifiers or
overloaded operators) known as methods,
constructors and destructors.
Members are declared publicly or
privately accessible using the public: and
private: access specifiers respectively.
#include <iostream.h>
#include <string.h>
using namespace std;
class employee
{
public:
string name;
int age;
};
int main()
{
employee x, y;
x.name = "ABC";
y.name = "XYZ";
x.age = 25;
y.age = 19;
cout << x.name << ": " << x.age << endl;
cout << y.name << ": " << y.age << endl;
return 0;
}
Structures vs.
Classes
The only difference between a structure and
a class is that, in a class, the member data
or function are private by default whereas,
in a structure, they are public by default.
The following c++ code
Class sample
{
private:
int x;
public:
void func(void);
};
can be written as:
class sample
{
int x;
public:
void func(void);
};
The keyword private
isn't necessary because, in a class, the
members are
private
by default. The code segment can be modified
using a structure in
the following way:
struct sample
{
void func(void);
private:
int x;
};
The keyword public
isn't necessary because the structure
members are public
by default
|