C++ equivalents of Java things

I'm learning C++, having mostly programmed in Java.
I want to know what similar constructs C++ has for:

1. Vector (ie java.util.Vector)
In particular, I'm looking for a linear data structure which can be variable in length. I'm not concerned about heterogeneous elements, at least for the moment. I'm hoping I don't have to create and manage my own linked list.

2. packages
What is used to organize files into their respective groups?
e.g. I might have a package called plants, which contains the packages tree, grass, and flower, which, respectively contain Oak.java, Fescue.java and Daisy.java

3. structs vs classes
In Java, I might create a class, let's go with Oak, which has members:
int age;
int numberOfAcorns;
boolean hasLeaves;

In C++, I can do the same, or I can use a struct.
Why would I choose on or the other?

Comments

  • : I'm learning C++, having mostly programmed in Java.
    : I want to know what similar constructs C++ has for:
    :
    : 1. Vector (ie java.util.Vector)
    : In particular, I'm looking for a linear data structure which can be variable in length. I'm not concerned about heterogeneous elements, at least for the moment. I'm hoping I don't have to create and manage my own linked list.

    [blue]C++ has [italic]vector[/italic] containers as well as many other types of dynamically sizable containers (list, deque, queue, stack) that are a part of the STL (Standard Template Library).[/blue]

    :
    : 2. packages
    : What is used to organize files into their respective groups?
    : e.g. I might have a package called plants, which contains the packages tree, grass, and flower, which, respectively contain Oak.java, Fescue.java and Daisy.java

    [blue]Sounds like your describing the property of object inheritance that would be handled via the use of classes in C++.[/blue]

    [code]class plant
    {
    ...
    };

    class tree : public plant
    {
    ...
    };

    class grass : public plant
    {
    ...
    };

    class flower : public plant
    {
    ...
    };

    // etc...[/code]

    :
    : 3. structs vs classes
    : In Java, I might create a class, let's go with Oak, which has members:
    : int age;
    : int numberOfAcorns;
    : boolean hasLeaves;
    :
    : In C++, I can do the same, or I can use a struct.
    : Why would I choose on or the other?
    :

    [blue]Yes, you can do the same in C++. Using the inheritance property I discussed above, some of the members would belong to the base class while the more specific members unique to the type of grass/flower/tree we were dealing with would be members of the derived class.

    [code]class plant
    {
    int age; // Age is common to all plants so it would likely be a member of the base class
    public:
    void setAge(int value) { age = value; }
    };

    class tree : public plant // A tree is a type of plant
    {
    // Member variables/functions(methods) specific to a tree
    };

    class oak : public tree // An oak is a type of tree which is a type of plant
    {
    int numberOfAcorns;
    public:
    void setNumberOfAcorns(int value) { numberOfAcorns = value; }
    };

    oak myoak; // Create an instance of an oak
    myoak.setNumberOfAcorns(100); // Give "myoak" 100 acorns
    myoak.setAge(246); // Make "myoak" 246 years old[/code]

    A C++ [italic]struct[/italic] and a C++ [italic]class[/italic] are exactly the same except for the default member access which is public in a [italic]struct[/italic] and private in a [italic]class[/italic].[/blue]
  • : [blue]Sounds like your describing the property of object inheritance that would be handled via the use of classes in C++.[/blue]
    I can see how you would come to this conclusion, but what I'm talking about is different. I'm talking about filesystem structure as opposed to class structure. Yes, I would construct the classes as you have suggested, but this is not the structure of the filesystem.

    [blue]: A C++ [italic]struct[/italic] and a C++ [italic]class[/italic] are exactly the same except for the default member access which is public in a [italic]struct[/italic] and private in a [italic]class[/italic].[/blue]
    I think there a more differences than that. Afaik, you cannot instantiate a struct. I could be wrong, though.
  • : : [blue]Sounds like your describing the property of object inheritance that would be handled via the use of classes in C++.[/blue]
    : I can see how you would come to this conclusion, but what I'm talking about is different. I'm talking about filesystem structure as opposed to class structure. Yes, I would construct the classes as you have suggested, but this is not the structure of the filesystem.

    [green]Ok, I don't know Java so I was just guessing, maybe someone else could help with that question.[/green]

    :
    : [blue]: A C++ [italic]struct[/italic] and a C++ [italic]class[/italic] are exactly the same except for the default member access which is public in a [italic]struct[/italic] and private in a [italic]class[/italic].[/blue]
    : I think there a more differences than that. Afaik, you cannot instantiate a struct. I could be wrong, though.
    :

    [green]You can instantiate a struct just as easily as a class. A struct can also have member functions and constructors/destructors just like a class, as an example:

    [code]struct person
    {
    int age;
    string fname;
    string lname;
    // Constructor for type "person"
    person( string first = "John", string last = "Doe", int yrs = 30 )
    {
    fname = first;
    lname = last;
    age = yrs;
    }
    };

    ...

    // Instantiate objects of type person
    person person1("Bob","Smith",20); // Create a person "Bob Smith, age 20"
    person person2; // Default person "John Doe, age 30"[/code][/green]
  • : I think there a more differences than that. Afaik, you cannot instantiate a struct. I could be wrong, though.
    :


    you are incorrect. You can instantiate structs -- its done all the time. They can do everything that a c++ class can do
    [code]
    struct base_struct
    {
    public:
    base_struct() {this->m_x = this->m_y = 0;}
    int getX() {return this->m_x;}
    int getY() {return this->m_y;}
    void setX(int x) {this->m_x = x;}
    void setY(int y) {this->m_y = y;}
    private:
    int m_x;
    int m_y;

    };

    struct inheited_struct : public base_struct
    {
    int z;
    };
    [/code]

  • : : : [blue]Sounds like your describing the property of object inheritance that would be handled via the use of classes in C++.[/blue]
    : : I can see how you would come to this conclusion, but what I'm talking about is different. I'm talking about filesystem structure as opposed to class structure. Yes, I would construct the classes as you have suggested, but this is not the structure of the filesystem.
    :
    : [green]Ok, I don't know Java so I was just guessing, maybe someone else could help with that question.[/green]
    :

    I believe the C/C++ equivalent concept to Java package is a lib - ie. several functions/classes bundled together and usable by other programs.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories