Wednesday, September 1, 2010

Classes in c++

History of C++

Bjarne Stroustrup of Bell Labs extended the C language to be capable of Object-Oriented Programming (OOP), and it became popular in the 1990's as C++. There were several enhancements, but the central change was extending struct to allow it to contain functions and use inheritance. These extended structs were later renamed classes. A C++ standard was established in 1999, so there are variations in the exact dialect that is accepted by pre-standard compilers.

Public and Private parts

All member fields in a class are private by default (no code outside the class can reference them), whereas fields of a struct are public, meaning that anyone can use them. For example,
struct Product {
char mfg_id[4]; // 4 char code for the manufacturer.
char prod_id[8]; // 8-char code for the product
int price; // price of the product in dollars.
int qty_on_hand; // quantity on hand in inventory
};
could be rewritten as a class like this
class Product {
public:
char mfg_id[4]; // 4 char code for the manufacturer.
char prod_id[8]; // 8-char code for the product
int price; // price of the product in dollars.
int qty_on_hand; // quantity on hand in inventory
};

No comments:

Post a Comment