-5

Does the Bclass become memeber of Aclass like a variable? And the my_obj of Aclass takes 5 as input?

ArrayList class starts with:

template <typename E>
class ArrayList: public ListInterface<E>{
public:
    ArrayList(int capacity){
        listArray_ = new E[capacity]; 
        n_=0; 
        capacity_ = capacity;
    }...

SortedArrayList class starts with:

template <typename E>
class SortedArrayList: public ArrayList<E>{
public:
SortedArrayList(int capacity): ArrayList<E>(capacity) {
} ...

In the int main:

SortedArrayList<myFavoriteDataType> my_list(5);
my_list.add(myFavoriteDataType("P17", 17)); 
my_list.add(myFavoriteDataType("P5", 5)); 
my_list.add(myFavoriteDataType("P3", 3)); 
my_list.add(myFavoriteDataType("P100", 100)); 
my_list.add(myFavoriteDataType("P24", 24)); 
my_list.print();

So now I need to create class myFavoriteDataType class. My teacher did not explain what does the SortedArrayList mean?

phoxd
  • 1,546
  • 3
  • 12
  • 26
  • `Bclass` is a template argument in your example. – Kerrek SB Sep 18 '17 at 23:13
  • @juanchopanza Well, let's ask the OP a conscious question first. – user0042 Sep 18 '17 at 23:33
  • @user0042 no, no. he has the right of it. I should at least take this seriously. – user4581301 Sep 18 '17 at 23:40
  • @KerrekSB How to declare Bclass so that Aclass works ? – phoxd Sep 18 '17 at 23:49
  • This depends a lot on `Aclass` and `Bclass` and what they are supposed to do. Any question about how to make something work should include an example of the code you have and a description of the problem you have with it. Error messages and warnings from the compiler are always a plus. – user4581301 Sep 18 '17 at 23:52
  • @user4581301 I updated, is it sufficient information? – phoxd Sep 19 '17 at 00:06
  • You need a bit more. I can infer that `listArray_` is `E * listArray_`, but you should remove all doubts. Sometimes the problem you are having is this itty-bitty, little detail that you left out of the example because you didn't know it was important. Like the `*` in `E * listArray_`. – user4581301 Sep 19 '17 at 00:24
  • Unrelated: From this example it looks a like the person who prepared the course material may be more familiar with Java than C++. Save yourself some pain and read up on the [Member Initializer List](http://en.cppreference.com/w/cpp/language/initializer_list) and the [Rules of 0, 3, and 5](http://en.cppreference.com/w/cpp/language/rule_of_three) – user4581301 Sep 19 '17 at 00:58

1 Answers1

2

Aclass is a class template being specialized around Bclass. This means that a new class will be constructed replacing tokens where the template's parameter is found with Bclass. It is possible that there are multiple template parameters, and in this case A) the code is attempting a Partial Template Specialization and B) it will not compile.

Example:

template <class TYPE>
class Aclass
{
    TYPE data;
public:
    Aclass(int num): data(num)
    {
    }
};

The above will create a class that stores a type yet to be specified, TYPE, and attempt to initialize it with the number provided by the constructor.

Or something like

class AclassOfBclass
{
    Bclass data;
public:
    Aclass(int num): data(num)
    {
    }
};

But you'll never see this. It just lives inside the compiler. The compiler creates it, uses it, and then spits out machine code based on it.

The number of different ways Aclass could use TYPE is staggering. Most often, yes, it is used to set the type of a member variable, but if you want to see how far this can go, look up template metaprogramming

Now that we have an Aclass specialized around a Bclass, it is being instantiated as an object named my_obj constructed with the number 5. Working from the above example,

class Bclass
{
    int value;
public:
    Bclass(int num): value(num)
    {
    }
};

int main()
{
    Aclass<Bclass> my_obj(5);
}

which results in my_obj ultimately containing the number 5. This is hugely not a useful example. So it's great that you narrowed the scope of your question with some code.

SortedArrayList<myFavoriteDataType> my_list(5);

makes a SortedArrayList, which I presume sorts its contents, named my_listthat stores a list of myFavoriteDataType that is 5 elements long.

user4581301
  • 33,082
  • 7
  • 33
  • 54
  • Thank you, I think I understand templates more now. In the end, I found that teacher just wanted to for us to overload binary operations for MyFavoriteDataType so that there are no conflicts with SortedArrayList. Almost every student spent more time understanding her objective than actually writing code. So, you mentioned template metaprogramming, is it where I can learn this kind of programming? – phoxd Sep 19 '17 at 01:44
  • [There are several books here in the intermediate and advanced sections.](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) Make sure you have a good grasp on C++ fundamentals before diving in. – user4581301 Sep 19 '17 at 02:37