disarikan dari sini dan sini
not yet, class design
Ide dasar kelas:
encapsulation: to make programs more modular
– the outside world doesn’t need to know exactly what data is stored inside the class
– the outside world just need to know which functions it can use to access the data
– advantage: allow the implementation to change more easily
>> because nobody have to rely on it (the detail of a class), except the class itself
>> Kelas: koleksi data, related to a single object type [house]
– include information regading real world object –> variables [items inside house]
– include functions [door] to access the data
– posses the ability to inherit from other classes
Make sure to know the difference(s) between:
– instance
– class member
– instance variable vs class variable
– instance method vs class method
Member of a class:
– data / member variable / (instance variable) / class attribute / property / field
– function / method
Other term (please check later):
– instance variable -> similar to class variable (but not the same)
– instantiated object
– instance = object?
– object instance
– invoke an object = instantiate an object/create an instance?
– copies of instance variables
need to consider sentence:
– all instances of an object have their own copies of instance variables
– one object instance can change values of its instance variables without affecting all other instances
– instance variables can be used by all methods of a class unless the method is declared as static
– a class of objects that describes a set of objects
Syntax
– keyword class
– nama kelas
– open bracket {
>> degree of restrictions: either public, procted, private
— public
— protected: functions outside the class to access the variable
— private (similar to protected, but will be different, related to inheritance)
– restriction keyword, colon :
– variables
– functions (usually prototype –> why?), ended with semicolon ;
>> two basic and need functions (neither return arguments/return value):
— Constructor, className
—– to initialize variables
—– if you don’t need to perform any initialization, the compiler will create a default constructor
— Destructor, ~className
—– to clean up after the class, including freeing any memory allocated
—– if you don’t need to do anything special in the destructor, the compiler will create a default destructor
Why separate /having degree of restrictions?
>> it makes the class easier to use
– the key idea is to separate
— the interface you use
— the way the interface is supported
— the way the interface is implemented
How it works
1. Programmer declares an instance of the class
2. Constructor will be automatically called
3. Destructor is called when the class is no longer usable
>> when the instance of the class is no longer needed, e.g.:
– program ends
– class reaches the end of scope
– memory is deallocated using delete
contoh, diambil dari cprogramming.com (diketik ulang)
#include<iostream>
using namespace std;
class Computer
{
public:
Computer();
~Computer();
void setspeed(int p);
int readspeed();
protected:
int processorspeed;
};
then
Computer::Computer() //constructor --> can accept arguments
{
processorspeed = 0
}
Computer::~Computer()
{
}
void Computer::setspeed(int p)
{
processorspeed=p;
}
int Computer::readspeed()
{
return processorspeed;
}
finally
int main()
{
Computer compute; // create an instance
compute.setspeed(100);
// means that it assign computer.processorspeed (protected)
cout<<compute.readspeed();
// calling a public func readspeed who return value from (protected) var processorspeed
}
CLASS DESIGN
- Decide the public interface for the class