I'm having trouble understanding classes. Could someone please help me out with them. I mean just a way to make the tutorials seem easier to understand, anything will do.
Printable View
I'm having trouble understanding classes. Could someone please help me out with them. I mean just a way to make the tutorials seem easier to understand, anything will do.
A class is like a structure that has functions
declare a class like this
that's the basics, i guessCode:class [class name]
{
public:
Member Functions
Constructor
Destructor
private:
Variables
};
The variables can only be accessed through the member functions
declare member functions
[class name]::[function name](paramaters)
{
//stuff
}
use class
int main()
{
//create an instance of class
[class name] variable;
variable.function(paramaters);
}
In addition to the above:
There are three levels of access in classes (by classes, I mean classes or structs):
public, private, and protected
Any symbol (typedefs, functions, variables, member classes, etc.) that are declared public can be accessed by anything. Those symbols which are declared as private can only be accessed by the class itslef, and classes/functions declared to be 'friends' of the class (not even derived classes have access to these). Any symbol that is protected can be accessed by friends and derived classes.
Constructors (that have the same name as the class, and any arguments you choose) are initializers. They are responsible for creating an instance of the class, and constructing its 'this' pointer. Desctrutors (same name, prefixed by '~') take no arguments, and destroy the object.
Neither the constructors nor the destructor need to be public, and as it is possible to have a static build function in a class, as well as have the class commit suicide (delete the 'this' pointer). These are rarely used, however.
One other thing. The difference between a class and a struct in C++ is simply that if you don't provide an access specifier (private, protected, public), then a class defaults to private, and a struct defaults to public.
This is a rather rapid (and incomplete) overview, but a start, I suppose.
A class is a user-defined datatype. Itīs through classes you can create ADT (abstract datatypes). ADT are used to make computerlanguage more humanlanguage (in simple terms). I will illustrate this with an example
Lets say you want to code a program that handles infomation about employes in a company. Without classes
with classesCode://Simulate an employe-object
//Code snippet 1
char *name = "Smith";
int age = 28;
double salery = 40000.0;
Now of you study the code snippet 1 you can see that the information is scattered (it nothing that keeps all infomation together). This can (will) lead that the code is gonna be hard to read and maintain, especially when a program grows in size. But when using classes (code snippet 2) you actually encapsulate all necessary infomation to a single unit. But code snippet 2 only declares (dont allocates memory) what an employee is. If you want an employee-object (an instance) you have to define one.Code:class employee
{
//Code snippet 2
//Methods should be public
public:
employee(char *pName = "Smith", int pAge = 28, double pSalery = 40000.0);
//Datamembers should be private
//private:
char *mname;
int mage;
double msalery;
};
and to acces the elements of an employee-objectCode:employee Eric;
Classes is a big subject and donīt feel stupid if you donīt understand everything I said. It is hard.Code:std::cout << "Employee name is: " << Eric.mname << std::endl;
std::cout << " and is " << Eric.mage << " years old" << std::endl;
std::cout << "and has a salery " << Eric.msalery << std::endl;
I personlly like books more than tutorials because they have more depth.