• Advertisement
Sign in to follow this  

I need desparate c++ help! (thanks to all the ppl who tried to help me earlier)

This topic is 4592 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

After some time realising there was no point using a game engine or modding games...I realised that there was one solution....learn c++! I'm learning and zipping through my online tutorial but I occasionally meet some problems....these tutorials don't give very good explanations! I need the basics of object orientation completely re-explained....starting from public, protected and private classes. I forgot....thanks to all the people that tried to help me in my earlier topic!

Share this post


Link to post
Share on other sites
Advertisement
Give us your age and what games you've been playing recently, as well as your computer specs and any game systems you own.

Then tell us what compiler and tutorial you are working with.

Share this post


Link to post
Share on other sites
OK then....

I am around 13 years of age....I'm using the intap.net tutorial, I used to use cprogramming.com but its explanations weren't that good as well....I'm using a Pentium 4 Windows XP Professional computer....any more info?

recently edited: I'm using Dev-c++ 4.0 compiler.

Share this post


Link to post
Share on other sites
Hmm well don't rely on just one tutorial try searching the weab as you've probably heard before google is your buddy! :P
There are plenty of articles on this site as well if you haven't noticed...

Share this post


Link to post
Share on other sites
Quote:
Original post by ReneGade RG dev
OK....I'll try to find more and I'll come back for help if I don't find anything good....


Here's something ... and here try and see if those are any help. If you can you should get a book, tutorials always leave out a lot of details not the best way to learn IMO

Share this post


Link to post
Share on other sites
The best way to learn is using a book, following along as you read by writing code which illustrates what the books says. See if you can check one out from a local library!

Share this post


Link to post
Share on other sites
Quote:
Original post by Boder
Give us your age and what games you've been playing recently, as well as your computer specs and any game systems you own.


I'm curious as to how this has anything to do with the OP's problem.

Share this post


Link to post
Share on other sites
I've tried another tutorial....covered some points I was confused on but made me confused in other bits...what exactly is polymorphism?

What is a virtual method?

Share this post


Link to post
Share on other sites
Quote:
Original post by Thunder_Hawk
Quote:
Original post by Boder
Give us your age and what games you've been playing recently, as well as your computer specs and any game systems you own.


I'm curious as to how this has anything to do with the OP's problem.


I'm terrified he posted this in? Does this guy wanna hack into my PC? Is this guy onto me? **shakes in terror**
Thunder_hawk....you're not the only one curious

Share this post


Link to post
Share on other sites
Quote:
Original post by ReneGade RG dev
I've tried another tutorial....covered some points I was confused on but made me confused in other bits...what exactly is polymorphism?

What is a virtual method?


These are considered somewhat advanced topics. You're jumping too far ahead. Stick with beginner tutorials.

Also stop double posting. This isn't a chat room, it's a forum. We are here to help you, but we have other stuff to do as well.

Edit: Triple posting.

Share this post


Link to post
Share on other sites
LOL. I'm not trying to hack you. And those questions have nothing to do with the OP's current problem, which is actually a demand: "I need the basics of object orientation completely re-explained....starting from public, protected and private classes." Rather, those questions were asked to get a better feel of where this new member is coming from.

Share this post


Link to post
Share on other sites
To the OP:

I can sympathize with your position... I'm 15 and just started C++ around a month and a half ago. I tried at first with just online tutorials but, honestly, they cannot compare to a good book or two. Personally I had two books that I went through. If I didn't understand a concept in one of them, the other usually cleared it up for me. The explanations are miles ahead of most online tutorials and it's just nice to be able to kick back on the couch and read a book instead of staring at the computer screen for hours on end.

That said, I realize getting a book might not be that easy. However, it's very difficult to explain such broad topics as polymorphism in the confines of a post. Both of the books I read set aside one or more whole chapters to fully describe it. There's a reason online tutorials aren't as good as books... there's only so much people are willing to write for free =P If you can't find a book, I recommend that you try to at least grasp the basic concepts and then ask question about specific details. For example, polymorphism is a concept and is tough to describe in a post. Virtual functions, however, are easier.

Going to need some help here from others, but if memory serves I think virtual functions are for use with derived classes. Say you want to use a Draw() function for class Shape and derived classes Square and Circle. By declaring the Draw() function virtual in the base class, you can redefine the Draw() function in the derived classes and have the decision made at runtime which version to use.

So say you want to refer to a bunch of Shape objects through pointers.

Shape* Circ1 = new Circle;
Shape* Square1 = new Square;


Note: Referring to derived classes through a base class pointer is another section of polymorphism, but for now, just know that you are able to do it.

Then you want to call Draw() on both Circ1 and Square1. Well, how is the program supposed to know which version to use? The answer is that, if Draw() is declared as virtual, the decision will be made at runtime by the program. Even though Circ1 is declared to point to a Shape object, the program is capable of deciding what type of object the pointer actually points too, in this case a Circle. This is the main use of virtual functions.

Anyway, as you can see, these things are very hard to explain on message boards, which is why you are unlikely to get any good answers to questions about broad topics. Do your best to grasp the basic concepts and then come back and ask specific questions. I'm sure many people here will be willing to help.

Share this post


Link to post
Share on other sites
Quote:
Original post by Scet
Quote:
Original post by ReneGade RG dev
I've tried another tutorial....covered some points I was confused on but made me confused in other bits...what exactly is polymorphism?

What is a virtual method?


These are considered somewhat advanced topics. You're jumping too far ahead. Stick with beginner tutorials.

Also stop double posting. This isn't a chat room, it's a forum. We are here to help you, but we have other stuff to do as well.

Edit: Triple posting.


All the tutorials I go on cover this!



Quote:
To the OP:

I can sympathize with your position... I'm 15 and just started C++ around a month and a half ago. I tried at first with just online tutorials but, honestly, they cannot compare to a good book or two. Personally I had two books that I went through. If I didn't understand a concept in one of them, the other usually cleared it up for me. The explanations are miles ahead of most online tutorials and it's just nice to be able to kick back on the couch and read a book instead of staring at the computer screen for hours on end.

That said, I realize getting a book might not be that easy. However, it's very difficult to explain such broad topics as polymorphism in the confines of a post. Both of the books I read set aside one or more whole chapters to fully describe it. There's a reason online tutorials aren't as good as books... there's only so much people are willing to write for free =P If you can't find a book, I recommend that you try to at least grasp the basic concepts and then ask question about specific details. For example, polymorphism is a concept and is tough to describe in a post. Virtual functions, however, are easier.

Going to need some help here from others, but if memory serves I think virtual functions are for use with derived classes. Say you want to use a Draw() function for class Shape and derived classes Square and Circle. By declaring the Draw() function virtual in the base class, you can redefine the Draw() function in the derived classes and have the decision made at runtime which version to use.

So say you want to refer to a bunch of Shape objects through pointers.

Shape* Circ1 = new Circle;
Shape* Square1 = new Square;


Note: Referring to derived classes through a base class pointer is another section of polymorphism, but for now, just know that you are able to do it.

Then you want to call Draw() on both Circ1 and Square1. Well, how is the program supposed to know which version to use? The answer is that, if Draw() is declared as virtual, the decision will be made at runtime by the program. Even though Circ1 is declared to point to a Shape object, the program is capable of deciding what type of object the pointer actually points too, in this case a Circle. This is the main use of virtual functions.

Anyway, as you can see, these things are very hard to explain on message boards, which is why you are unlikely to get any good answers to questions about broad topics. Do your best to grasp the basic concepts and then come back and ask specific questions. I'm sure many people here will be willing to help.



I understand....which book should I get?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
The online tutorials should have covered this already I guess... somewhere.


class A
{
public:
int ImPublic;
private:
int ImPrivate;
protected:
int ImProtected;
...
...
}

class B : public A
{
B()
{
ImPublic = 1; // OK
ImProtected = 1; // OK
ImPrivate = 1; // not OK, is private to class A
}
}

main()
{
A a;
a.ImPublic = 1; // OK
a.ImPrivate = 1; // not OK, private cannot be
// accessed outside of class A
a.ImProtected = 1; // not OK, protected is private, except to
// enhareted class.
}



Public can be accessed anywhere, by any object that has access to the class object. Private members can only be accessed by the class itself. Protected member can only be accessed by the class itself, or an enhareted class.

I think I got that right.

Share this post


Link to post
Share on other sites
Tested, commented, and correct usage of example. I didn't login so I can't edit last post.


class A
{
public:
A() {}
~A() {}

int ImPublic; // public variable.
// public methods for accessing private and protected data from outside the class itself.
void SetPrivate(int inval) { ImPrivate = inval; }
void SetProtected(int inval) { ImProtected = inval;}
void ShowOff() // display values.
{
std::cout << ImPublic << std::endl << ImPrivate << std::endl << ImProtected << std::endl;
}
private:
int ImPrivate; // private variable.
protected:
int ImProtected; // protected variable.
};

class B : public A
{
public:
B()
{
ImPublic = 2; // public is public. :)
SetPrivate(5); // cannot access A's private variable, must use public interface
ImProtected = 10; // protected is accessed by A and B the same as public, but is
// considered private to everyone else.
}
~B() {}
};


int main()
{
A a;
B b;

// "main()" is accessing these, not "a"
a.ImPublic = 1; // public is a 'free-for-all'
a.SetPrivate(2); // must use public interface "SetPrivate"
a.SetProtected(3); // must use public interface "SetProtected"

std::cout << "A" << std::endl;
a.ShowOff();
std::cout << "B" << std::endl;
b.ShowOff();

system("pause");
return 0;
}


Share this post


Link to post
Share on other sites
Quote:
Original post by ReneGade RG dev
After some time realising there was no point using a game engine or modding games...
Rubbish.

How do you make broad, sweeping assessments about something you barely know? The dismissiveness and arrogance some of you bring to this endeavor - which you are ostensibly trying to learn - is frightening.


In any case, object orientation is itself comprised of a number of concepts: encapsulation, data hiding, inheritance and polymorphism.
  • Encapsulation is the process of wrapping up a number of distinct but related data and operations in a single class definition. Say you are implementing a 4x4 rectangular matrix type; your distinct data are rank and the 16 coordinate values while your operations include addition, subtraction, scalar multiplication, matrix multiplication, rank extraction (necessary for matrix multiplication, as an M x N can only be multiplied by an N x P matrix, yielding an M x P result), transpose, inverse and a variety of determinants, among others. By placing all of these in the class definition, you have effectively encapsulated the notion of a matrix in your object.

  • Data hiding is rooted in the fact that some of the data in your class definition may be necessary for its inner workings, but should not be modifiable by external entities. In addition, a piece of data may need to always be valid, necessitating a distinction between the public view of this data and the internal, private view. To this end, all object-oriented languages support a means of hiding certain data from external code (though it can always be circumvented with enough effort, but it's not a security mechanism anyway). For instance, the rank information in the preceding matrix example should be hidden from external modification but seamlessly deployed to instantiate the rank of the result of a matrix multiplication.

  • Inheritance is a powerful but overused and difficult tenet of object orientation. Simply put, it is the ability to define a class as being based upon an existing class, inheriting properties and operations from the existing class. C++ inheritance is particularly difficult because of access specifiers; Python inheritance is much simpler, so it may help to look at a Python example to grasp the fundamental concept.

    Another problem in learning inheritance is that the vast majority of examples you will be presented with are convoluted in the extreme, and only serve to illustrate a language feature, not when or why you'd actually use it.

  • Polymorphism is often viewed as being intrinsically related to inheritance, but this isn't true. The term polymorphic means "of many forms," which provides insight into a true understanding of the concept. Polymorphism refers to the ability of a type or operation to behave in the manner of another type or operation, allowing for the creation of code that doesn't care what the actual type of an object is. In the inheritance vein, polymorphic dispatch is the ability to invoke more specific behavior in a descendant class Derived via an interface to a more generic class Base. In the generic view, on the other hand, being able to call an operation X on an argument v without caring about the specific type of v, and obtaining type-specific behavior is polymorphism, too.

    With the introduction of templates, C++ has acquired generic polymorphism, which many will admit can be more powerful than polymorphic dispatch because of the ease of extension (implement a specialization for your type versus deriving a class and maintaining proper interface semantics while providing a specialized implementation), and because it lowers the coupling of the overall system.

That's object orientation in a nutshell.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
He is 13. Thats where the arrogance comes from.

In order for you to make your game(the mmorpg fromt the other thread) without modding or using someone elses code you will need to:

Learn a languane well - C++ u are working on
Learn the windows api
Learn some sort of networking api as well as
the theory behind it
Learn the basics of 3d graphics and on the way
linear albebra, trig, ...
Learn a 3d graphics api
Learn an input and sound api
and probably another thousand things along the way.

Get a good book and work through it. There is a reason people
go to college for this kind of stuff

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement