Hi, I have done some c++ programming and used structures as opposed to classes as I understood those. My problem is understanding Classes. I have read many articles online but still cannot fully understand the concept. Is there anyone that can explain the class and its processes? or maybe has an article that has an easier explanation?. Thanks.
struct and class structures are exactly the same, but with different names and different default access-specifiers. struct is still in use to allow backwards compatibility with legacy C code.
Thanks for your reply. Its difficult to explain troublesome areas when I dont know what to ask without understanding something. I understand that the class is a template then you use an instance of that to work with. its how the private and public sections work as well as how the declared functions or methods work within the class declaration and how those are used within main(). Hope this makes some sense.
private:
Any data members or methods (functions which are part of a class) below the private access-specifier are inaccessible to any function or variable that is not associated with the class. For example:
1 2 3 4 5 6 7 8 9
class Simple
{
private:
int Member;
} NewSimple;
int Variable( NewSimple.Member );
// The above statement is illegal since Simple::Member is private.
public:
Any data members or methods below the public access-specifier are accessible to all variables and functions that are not associated with the class. This breaks encapsulation. For example:
1 2 3 4 5 6 7 8 9 10
class Simple
{
public:
int Member;
} NewSimple;
int Variable( NewSimple.Member );
// The above statement is now legal since Simple::Member is
// now publicly visible.
protected:
The protected access-specifier is different. Its functionality kicks-in during inheritance. Any data members or methods within the inherited can access the protected members of the base class. However, any non-inherited data member/variables or methods/functions cannot access them. For example:
class Base
{
protected:
int Member;
};
class Child : public Base
{
public:
void Print( )
{
std::cout << Member << std::endl;
}
};
Base NewBase;
int Variable( NewBase.Member );
// The variable initialization statement is illegal. However, the
// std::cout statement is legal because Child inherits Base's
// members.
The descriptions could be better but my sides are aching from watching Lee Evans :)P