Classes
- Template to define the form of an object
- Specifies both code and data, functions and varibles.
class classname{
int privatedata;
int privatefunction();
public:
int publicdata;
int publicfunction;
}
Private/Public
- By default, any function or varible will be private
- Everything after the "public:" command is public
Accessing Varibles & Functions
classname.publicdata = 5;
classname.publicfunction();
Functions in Classes
- Functions must be declared in the desired secion of the class (public or private)
- Scope operater is used to tell the program which function belongs to which class.
int classname::publicfunction() {
...
}
Constructor
- Code to be run when the class is initalized. The destructor runs when the class is destroyed (eg. program terminates)
class classname{
classname(); //Constructor
~classname(); //Destructor
}
Paramterized Constuctors
- Parameters can be passed to construcors the same way as any other function.
class classname{
classname(int data);
}
classname::classname(int data){
...
}
classname object(5);
Comments (0)
You don't have permission to comment on this page.