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.
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.
The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.
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.
01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
Good things donīt come easy in life!!!