C++ Programming help

excuse me but what means that function is “virtual”?
also what means “=0” and “const” when placed after “)” in function declaration?

and how does inheritance in C++ (about functions in class when done on parent-type pointer)

I know allmost all non-classes basics of C++ (mainly those come from “C” and some about references and new use of struct w/o functions)
I know about very basic of classes but almost nothing about inheritance
I know even about templates and try-catch structures,

but as I said I have big lack in classes (mostly inheritance) and don’t understand some new keywords like virtual

Polecam Megatutorial.

Let’s say you have class A and class B that derives from A

class A{...};
class B : public A{...};

Additionally, you have a pointer to object of class A

A * pointer;

Of course, you can write

pointer = new B;

And now we have a difference between virtual and on-virtual functions: when classes A and B have two functions with the same signatures (names, return types, arguments etc.) and they are not virtual, when you call pointer->function();, the function from call A is executed. But if they are virtual, appropraite function is called, for example:

A * p1 = new A;
A * p2 = new B;

p1->function(); //function from class A is called
p2->function(); //function from class B is called

There is also a second meaning of virtual (that modifies inheritance) but the first is more popular.

=0 means that this function must be overwritten by all classes that derive from this class if you want to have objects of that classes. const means that this function receives pointer to this as pointer to const value (you cannot modify fileds).

Ehm… how could you know about templates when you know little about classes? Templates are far more complicated that inheritance and these two interfere in many ways.

Sometime I had got knowing all C++ part from C and very basic from classes, so when I saw two tome book about C++ the first was all basic+classes and second the rest (templates, exceptions + some more) so I bought second and know it have been easy

so not the classed are hard but I were uneducated in classes (mostly in inheritance - I known basic inheritance, but nothing about polymorphism etc.). Now I’m unhappy that so much have been lost - it was appearing as very little things - but weren’t. So without knowing the ‘magic’ of C++ I was still thinking C… but I get megatutorial and know now what i haven’t know before

templates are very nice for work with various type of data and avoid C-ish use of (void*) or troublesome macros
exceptions are nice in handling
the thing I most used from C++ what isn’t in C was operator functions - I did a math vector class called wektor which supported math and has additonal function for rotating - the class i used to ease physics handling in my project

now I have a question: Is it possible to make a template of class which all classes inherites from one pure virtual class? It would be helping to keep data which varies inside and have same function (I want to do a functional container whith main functions in base and the base class has an interface to do usual things - blitting moving giving forces etc - but objects have diffrent structure also i want to get one vector - container from stl - too keep all my physical objects or rather pointers to them… note that container have to contain one or at most a few class object and give interface for it contains). It would help overgoing the main template mancament that template classes is diffrient types

Could you recommend a good C++ tutorial (in English) on the web? :slight_smile: I have some time and would like a good, fairly comprehensive web tutorial or learning guide to C++ in English.

Or would you recommend I just translate the Polecam Megatutorial linked in this thread to English using Google or Babelfish?

Thanks in advance! :slight_smile:

Best regards,
Steven.

These may be not exactly ‘tutorials’ to learn step by step, but you will need detailed reference anyway:
en.wikibooks.org/wiki/C_Programming
en.wikibooks.org/wiki/C%2B%2B_Programming/TOC1
cplusplus.com/reference/clibrary/

I don’t know about C++ tutorials in English, I learned from Polish sources. Anyway, there is one good page about C++ I recommend: cplusplus.com/reference/ . It’s not a tutorial, but it contains a lot of useful information about this programming language. This page is very useful when you know the basics.

bump the question
Is it possible in C++ to make class template, which all class it contains have as a base class the same non-template abstract class (or some class with virtual functions for polumorphism)?

those would be a trick, but would ease much work on templates

What do you mean by ‘class template containing a class’ ?

All base classes must be specified by definition of the class (or template of a class). Does it answer your question?

I mean template classtemplate contains classes classtemplate, classtemplate, classtemplate etc.
maybe I used wrong word but I mean this

and by “is it possible” I mean - is it legal in rules of C++ language
and I ask it 'cos many things which are codeable aren’t supported by the language, so I ask if there’s limits not allowing to, or the place is clean?

You used wrong word once more. Paraphrase ‘contain’. Do you mean

  1. classtemplate inherits from classtemplate, classtemplate, classtemplate
  2. classtemplate contains fields of type classtemplate, classtemplate, classtemplate
  3. classtemplate has nested classes classtemplate, classtemplate, classtemplate
  4. something else ?

1 is impossible since to derive from a template of a class, full definition of that template must occur earlier. 2 is impossible because you cannot declare objects of undefined classes (class is defined after the end of its declaration), however you can have pointers / references to objects of mentioned types. 3 doesn’t make sense to me.

<snip(power lost)>

yeah, bad word
I mean the template class someTC isn’t the same as someTC
so perhaps better word would be “is made of” but in reverse direction
so classes are made of class template by compiler.

so i mean all classes made of the myclasstemplate<typename T, …> would inherit from the same mybaseclass which is not coming from template and have virtual members

I think of getting things by using both inheritance, and template techniques, and this is my first guess how to do it. If this could be thone or in other way, it could be able to do miracles in code :sunglasses:

EDIT: by using classes from template having various types as params it could be easier to divide code in smaller parts and be able to easy replace some. having one base class for template it could be done all basic actions defined in base class regardless of template parameters and use it by pointers regardless of which I use (the second come from inheritance-given abilities). even nesting would be fine - container doing something on member containers etc. and in last step doing it on all objects the last container include

would be

 all classes made of it 

Instantiated?

class otherClass
{
	virtual int foo(int x)
	{
		return x+5;
	}
};

template<typename T, int N>
class myClassTemplate : public otherClass
{
	int foo(int x)
	{
		return x+N;
	}
};

//...


	myClassTemplate<int, 666> * __pobj = new myClassTemplate<int, 666>();
	otherClass * pobj2 = __pobj;
	std::cout << pobj2->foo(5); //writes 671

Something like this?

exactly those structure of code (but of course other function set and templates parameter set :unamused: )
Nice that you get what i think, even if I wasn’t so communicative :smiley: