Click here to Skip to main content
15,900,468 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

Assume I have a separate header file with an associated .cpp file. Normally, if i wanted to do this with a regular class, to instantiate it in main, one would do:

C++
Class object;


This does not work with virtual classes, I understand this much. Despite reading, I am still not 100 percent sure why.

Using a pointer, does not work either as doing
C++
Class *object;


However:
C++
Class *object();
complies. Why is this? Is it working as intended?

What I have tried:

Everything mentioned above. I am new to C++, and even greener when it comes to using two separate files for classes (any tutorial that explains it well would be helpful). Throwing in virtual, confuses me even more.
Posted
Updated 17-Oct-16 21:55pm

Hello

For someone new to not only C++ but also C, it would be best to start with one or more tutorials. That would help you understand the most important basics and some basic program that you can then extend to try out more advanced stuff by yourself.

Here's a decent tutorial on Cplusplus.com, which is also a good reference site for advanced language features: C++ language tutorial

As to your questions:
1. a class that has one or more pure virtual functions cannot be instantiated. This is what is called a (pure) virtual, or 'abstract' class. Typically you use such classes if all you want to do is describing a specific behaviour, rather than the entire definition of the real class, or if you want to describe behaviour that is shared by several related classes.

Example:
C++
// file Shape.h

// class Shape describes some geometric object
// it may be drawn, but how to draw a shape depends on the specific type of shape
// therefore we define this class as pure virtual, and leave the drawing to the
// concrete shape classes:
class Shape {
public:
   virtual void draw() = 0; // pure virtual or 'abstract' member function
};

C++
// file Triangle.h

#include "Shape.h" // read declaration of the base class Shape

// class triangle
class Triangle : public Shape {
public:
   virtual void draw(); // this function will draw a triangle
};

C++
// file Triangle.cpp

#include "Triangle.h"

// implementation of the Triangle member functions
void Triangle::draw() {
   // draw some triangle here ...
}

(and now do the same for other types of shapes, e. g. Circle, Square)
C++
// file main.cpp

#include "Shape.h"
#include "Triangle.h"
#include "Circle.h"
#include "Square.h"

int main() {
   Shape* shapes[3];
   // construct the shape objects
   shapes[0] = new Triangle();
   shapes[1] = new Square();
   shapes[2] = new Circle();

   // now draw all shapes:
   for (int i = 0; i < 3; ++i) {
      shapes[i].draw();
   }

   // destroy the constructed objects again
   for (int i = 0; i < 3; ++i) {
      delete shapes[i];
   }

   return 0;
}

Now, if you could instantiate an object of class Shape and assign it to your shapes array, what do you think the call to draw() would do? You don't know? Well, the compiler doesn't either. That's why it is forbidden.

(rest of the questions in separate solution(s))
 
Share this answer
 
v2
2. The follwing line only declares a variable that can hold an address to an object of class Class:
C++
Class *object;

this is like a Street sign with the caption "Rome", lying on the ground: it is not yet pointing anywhere, and it is not the same as the city, Rome, at all. You can plant it in the ground and make it point in the right direction, but then it's still only a signpost, not a city.

3. The following line only declares a function that takes no arguments and returns a pointer to an object of type Class:
C++
Class *object();

Note that the trailing brackets constitute a valid function argument list, and therefore the compiler interprets object as the name of a function, which has an empty argument list.

These kinds of subtle differences are really important to understand, so I can only repeat my suggestion to first work your way through a tutorial.
 
Share this answer
 
The keyword virtual means that you cant instantiate the class. It is a rule in the C++ language. Virtual classes are often used as base class to define some behavior which is implemented in different class.
If you want to use a class in a different file you must include the header:
C++
#inlcude "class1.h"//

For using a pointer you also must create an objekt and delete it at the end.
C++
Class *object = new Class();//create instance
object->DoSomething();//call a function
delete object;// destroy it

A nice tutorial.
 
Share this answer
 
Quote:
I am new to C++, and even greener when it comes to using two separate files for classes

Then it's time to learn. you must be very confident with C++ compilation model. See, for instance: 1.9 — Header files « Learn C++[^].



Quote:
Throwing in virtual, confuses me even more.

Virtual (abstract) classes finds their usage in polymorphism. Now polymorphism is one of the pillars of OOP and you have to learn it in order to properly write C++ object oriented code. See, for instance: Polymorphism - C++ Tutorials[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900