from java to c++ constructors...

im trying to read some C++ code but i dont understand the constructor can someone please explain what the ampersand is doing(i know its not the address operator in this case I assume its same as && in java) in this constructor. I dont see the variable s is created any where so I guess that the constructor is creating a const string s?? im coming from programing in java so ..... I also dont understand how is it possible the shapetype variable is being used before it is declared.
maybe someone can comment this code up for me please explain it from a java programmer perspective... thanks in advance!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Shape
{
  public:
    Shape( const string & s ) : shapeType( s ) { } //comment here..
    virtual ~Shape( ) { }//destructor

    const string & getType( ) const //please comment this line for me
      { return shapeType; }

    virtual double getArea( ) const = 0;  //....
    virtual void print( ostream & out ) const //here
      { out << getType( ) << " of area " << getArea( ); }

  private:
    string shapeType;
};
I know very little java but I understand some of that.
Ampersand is the character used to define a reference in this case.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class Shape
{
  public:
    // This is a constructor. const string& s is a string that is passed into the 
    // class initialization. 
    // Example: Shape circle("circle");
    // ...: shapeType( s ) is the same as
    // shapeType = s;
    // Shape( const string & s ) : shapeType( s ) { } 
    // This could also be written like this:
    // Shape(const string& s)
    // {
    //     shapeType = s;
    // }

    Shape( const string & s ) : shapeType( s ) { } 
    virtual ~Shape( ) { }//destructor, I don't know anything about virtual functions.

    const string & getType( ) const //please comment this line for me
      { return shapeType; }
/*
Comment for const string & getType( ) const
This is a constant function that returns a reference to shapeType
*/

    virtual double getArea( ) const = 0;  //....I don't know what this means

    virtual void print( ostream & out ) const
      { out << getType( ) << " of area " << getArea( ); }
/*
virtual void print( ostream & out ) const
This function is a constant function that uses an output stream for a 
parameter. 
Example for use:
Shape.print( std::cout ) const
{
  out << getType() << " of area " << getArea(); // out = std::cout
}
*/
  private:
    string shapeType;
};


Sorry if that's a bad explanation. Maybe someone else knows both languages good and can explain it.
Last edited on
Hi !

Hope this helps:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Shape
{
  public:
    Shape( const string & s ) : shapeType( s ) { }
    // the & means it's not needed to create another objet with the same 
    // contents as the original object: it's a reference so that it if was modified here
    // it would also modify the object used by the calling function
    // (but it's made impossible because of the "const" decalaration

   // variables can be declared / used anywhere in the class
   // declaration order doesn't change anything

    virtual ~Shape( ) { }//destructor

    const string & getType( ) const
      { return shapeType; }
    // const keyword means the function can be used on a const object
    // so you can do: const Shape test(); test.getArea();
    // without compiler error

    virtual double getArea( ) const = 0;  
    // virtual ... = 0; means "pure virtual" function (which has to be redefined in classes that inherit from that one)

    virtual void print( ostream & out ) const // same
      { out << getType( ) << " of area " << getArea( ); }

  private:
    string shapeType;
};
Last edited on
Constructor:
const string & s The & means that the argument is passed by reference, not by value. Doing so you avoid to copy the object passes. const means that even if the argument is passed by reference, it can't be modified in the function
: shapeType( s ) this is the initializer list, the class members are initialized with the value given (in this case shapeType is initialized with the value of s (the constructor argument

const string & getType( ) const const string & The return type is a const reference of a string (same meaning of the argument in the constructor but in this case is the return type)
getType( ) const const means that the function doesn't modify the class instance (so it can be called for variable of type const Shape )

virtual double getArea( ) const = 0; virtual means that the function can have overrides (different bodies) on derived class
const is same as above
= 0; means that the virtual function hasn't the body in the base class, this makes the class abstract and it can't have objects

virtual void print( ostream & out ) const
Everything is as was for the function except for the fact that this has the body in the base class
ostream & out streams don't have the copy constructor so they must be passed by reference


In classes all members are visible not depending in which order they were declared

Some documentation: http://www.cplusplus.com/doc/tutorial/functions2.html
http://www.cplusplus.com/doc/tutorial/polymorphism.html

[Edit] Many answers at the same time...
Last edited on
ok i understand. basically in the constructor a constant string s object is created. than it is passed to a shapetype variable by reference.. its easier to read in this form
1
2
3
4
5
Shape(const string & s)
     {
         shapeType = s;
     }
     


and i didnt know that the spacing with the ampersand didnt matter ie..

Shape(const string & s) = Shape(const string& s)

thanks for the link to the functions2 it all helps...
Last edited on
I don't think the space matters. It's just style.
i.e. Some people write pointers like this
int *ptr = &a;

and some people write them like this
int* ptr = &a;
Last edited on
true, the spacing doesn't matter both versions compiled and run fine.


another question:
in this code is the << being passed by reference as an ostream type and overloaded at the same time?

1
2
3
4
5
ostream & operator<< ( ostream & out, const Shape & s )
{
    s.print( out );
    return out;
}
I think you are having difficulty understanding how arguments are passed. If you pass by value (string s), a copy of the original argument is made, and all changes in the function are made on that copy. When the function exits, all those changes are lost, which means the original variable isn't changed. This works fine on strings or basic variables, but when you pass large objects, making a copy can slow things down.
When you pass an argument by reference ( string &s), a copy doesn't have to be made, all changes are made on the original variable. This speeds up things, but the original variable can get messed up.
When you pass an argument by constant reference(const string &s), you get the best of both worlds, you get the speed of passing by reference( a copy doesn't have to be made), but you don't have to worry about the parameter getting changed by the function(consistency).
This is what Bazzy was telling you, I just thought a simpler explanation would help.
In C++, the example posted serves as a blue-print for a type. Alone, there are no class objects it only describes how objects of this class (once created) will "act".

It just so happens that the class is an abstract base class (identified by the pure virtual method). Classes that only contain pure virtual methods are similar to the interface concept in Java.

ok i understand. basically in the constructor a constant string s object is created. than it is passed to a shapetype variable by reference.. [...]


The use of the spoken language is very important here. Remember that the class represents a type, not an object. In the constructor, the pass-by-const-reference argument means to NOT create an object; it expects one to already be in existence and it can work on it directly (although, because of the const keyword, it is read-only). I think const is similar to the final keyword in Java.

Hope that helps.
eker676 you said that:

virtual void print( ostream & out ) const
This function is a constant function that uses an output stream for a
parameter.


are you sure you meant to say that? I understand pretty much everything here but I dont agree with the above quote. how is this print function constant the keyword const isn't in front of the print function so I dont see how its a constant function...

anyone....??
Last edited on
Now that I think about it... I am not sure if it means the return data is constant or the function is constant.
const used after the declaration of a function in that regard simply means that the member function will not modify any of the class members. It is a function that may be used in a read-only context.
can someone comment this code up for me?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <vector>
#include <functional>
using namespace std;
//template for the object
template <typename Object, typename Comparator>
void insertionSort( vector<Object> &arr, Comparator isLessThan )//
{
...........insertion sort algorithm not importans just 2 for loops...............
}
//another template for comparable
template <typename Comparable>
void insertionSort( vector<Comparable> &arr )
{
    insertionSort( arr, less<Comparable>( ) );
}


an important thing i figured is that the less is being used and can be used to represent <,>,<=,&>=.

The main thing i want to understand is templates. I'm new to templates. Comments from multiple users are welcome. Comment as much as you know.
Thank for the good insight guys. I have never even thought of doing that. Just pass everything either as a reference, or const reference. O_o lolz.
Topic archived. No new replies allowed.