Hi guys,

As my first post, i'd like to post something usefull instead of "Hi, i'm new here".
The reason for this topic is because i don't understand a few things, mostly about classes.
Here are the things i don't understand:
1) Why do some people set a "_" before things like function names?
I've seen this a few times, and asked myself if it had or didn't have any use.

2) For this one, you should look at a simple code:

class TutorialApplication : public ExampleApplication
{
protected:
public:
    TutorialApplication()
    {
    }

    ~TutorialApplication() 
    {
    }
protected:
    void createScene(void)
    {
    }
};

First i'd like to say that this isn't my code, second i'd like to say that somewere in the source code there is a class definded with the name ExampleApplication.
Now comes my question:
As you can see in the first line, after class TutorialApplication comes something else.
but what is the function of that? It confuses me.
3) My last question:
as you can see, there are 2 public functions defined:
TutorialApplication()(which i think is a constructor)
But there is also "~TutorialApplication()" which confuses me, what does it do? i mean with that strange "~" which i've seen for the first time...
EDIT: Another question: Why is on line 3 and 4 defined first protected and then public? does it have any function?
EDIT2: Another question: What can virtual functions do and why are they used?


Thanks in advance,
Tigran

Hi guys,
1) Why do some people set a "_" before things like function names?
I've seen this a few times, and asked myself if it had or didn't have any use.

A lot of this _class naming is done for library purposes, but it isn't restricted to that.


2) For this one, you should look at a simple code:

class TutorialApplication : public ExampleApplication
{
protected:
public:
    TutorialApplication()
    {
    }

    ~TutorialApplication() 
    {
    }
protected:
    void createScene(void)
    {
    }
};

First i'd like to say that this isn't my code, second i'd like to say that somewere in the source code there is a class definded with the name ExampleApplication.
Now comes my question:
As you can see in the first line, after class TutorialApplication comes something else.
but what is the function of that? It confuses me.

the : denotes an extension from another class. Since TutorialApplication extends from ExampleApplication, it will inherit all of the public and protected member fields of ExampleApplication and can be used as an ExampleApplication object.

3) My last question:
as you can see, there are 2 public functions defined:
TutorialApplication()(which i think is a constructor)
But there is also "~TutorialApplication()" which confuses me, what does it do? i mean with that strange "~" which i've seen for the first time...

The tilde ~ by the classname (like a destructor) denotes a Destructor. a Destructor is called when the delete operator or ~ObjectName::ClassName() line is called. Any code that is within the destructor when those modifiers are called, will be performed. This is to take care of floating pointers you may have in your class.

EDIT: Another question: Why is on line 3 and 4 defined first protected and then public? does it have any function?

There are access-modifiers in every class. They are of type public, protected and private (in java there's also package, but im not sure if that holds true in c++).

Anywho, the access-modifiers restrict access of the object.

If a modifier is marked private, only the class that contains that object may use/alter that object, nobody else.

If a modifier is marked protected, any object that extends from the object with protected members will have access to those members.

If a modifier is marked public, there is no restriction - everyone can use it!

Thanks in advance,
Tigran

Np, but I'm sure there are some loopholes in my explanations (gut feeling), so if there are any please let them be known (directed to other readers).

Thanks Alex,

I now understand what you just explained to me.
Maybe i edited the topic too late at the second time, cos it looked like you missed my second edit :P

The last question was the explanation of virtual functions. If you could explain that one last thing, i would be very gratefull, though i already am of you.

Thanks in advance,
Tigran

Virtual functions mark the function declared to be overwritten by a class such that the method will perform the functionality of the derived class when the base class is called...

For example...

#include <cstdlib>
#include <iostream>

using namespace std;

class ToString
{
      public:
                virtual char *className(){return "No Name Set!";};
};

class Person : public ToString
{
      private:
               char *myName;

      public:
              Person(char *nameArg){myName = nameArg;};
              char *className(){return myName;};
      
};

int main()
{
    Person matt ("Matt");
    ToString *ts = &matt;
    cout << ts->className() << endl;
   
    cin.get();
    return 0;
}

This should print out "Matt," despite the upcasting of Matt into ToString.

This is because we marked the method of className() in the base class virtual- a derived class's data that overrides the method with the same name and arguments will, at runtime, force the base class to call the derived class's method.

This is particularly useful if we want to make an array (or pointer) to objects of many types and we only want to call one method to perform various kinds of functionality.

Thanks Alex,

I now understand how virtuals work, i thank you very much for your effort.
And one more question: So can virtual fuctions actually perform actions when they are not overwritten?

Thanks in advance,
Tigran

Thanks Alex,

I now understand how virtuals work, i thank you very much for your effort.
And one more question: So can virtual fuctions actually perform actions when they are not overwritten?

Thanks in advance,
Tigran

Only if they aren't pure virtual.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.