Classes

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.
closed account (zb0S216C)
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.

What part of the class concept is troubling you?

Wazzak
It's important that you learn classes and polymorphism or you might as well be programming in C. Hopefully these should shed some light on it

http://cplusplus.com/doc/tutorial/classes/
http://cplusplus.com/doc/tutorial/classes2/
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.
closed account (zb0S216C)
Here's a rough overview of a class:

Access-Specifiers


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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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

Wazzak
Last edited on
Thanks for the assistance that has shed some light, especially to Framework for the detailed descriptions. Very much appreciated.
Topic archived. No new replies allowed.