10 Replies - 605 Views - Last Post: 06 May 2013 - 08:30 AM Rate Topic: -----

#1 irkzuptang  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 04-May 13

countryType class

Posted 04 May 2013 - 12:53 AM

Write a program that listed all the capitals for countries in a specific region of the world. Create a countryType class to store the information. This class will contain capital, primary languages and the population. Create a “continent” array of countryType class and display its contents.

i need help i don't have an idea how to do this. i don't know how to do an array of classes.

Is This A Good Question/Topic? 0
  • +

Replies To: countryType class

#2 Zereo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 43
  • View blog
  • Posts: 108
  • Joined: 08-November 12

Re: countryType class

Posted 04 May 2013 - 01:21 AM

A array of classes is just like a array of anything else almost.

struct Country
{
    string capital;
    string primaryLang;
    unsigned population;
};

int main()
{
    // A array full of 10 countries.
    Country countries[10];

    // Modify the first elements information
    countries[0].capital = "Washington";
    countries[0].primaryLang = "Lying";
    countries[0].population = 2;
}


We also need more information on what you are having trouble with doing. Just saying I need help doesn't tell us much. Post what you have done and what you don't understand and remember to be specific and we can help walk you through it.

This post has been edited by Zereo: 04 May 2013 - 01:24 AM

Was This Post Helpful? 3
  • +
  • -

#3 irkzuptang  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 04-May 13

Re: countryType class

Posted 04 May 2013 - 01:28 AM

View PostZereo, on 04 May 2013 - 01:21 AM, said:

A array of classes is just like a array of anything else almost.

struct Country
{
    string capital;
    string primaryLang;
    unsigned population;
};

int main()
{
    // A array full of 10 countries.
    Country countries[10];
}


We also need more information on what you are having trouble with doing. Just saying I need help doesn't tell us much. Post what you have done and what you don't understand and remember to be specific and we can help walk you through it.


How do you that in a class? you did it in a struct. Well, I haven't done anything yet, cause I don't know how to create a program with classes in it. And also, i don't know what is a mutator, an accessor and abstraction. In functions, should the syntax be always like this (sample only):

void clockType::setTime(int hours, int minutes, int seconds)

Was This Post Helpful? 0
  • +
  • -

#4 Zereo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 43
  • View blog
  • Posts: 108
  • Joined: 08-November 12

Re: countryType class

Posted 04 May 2013 - 01:45 AM

A struct and a class are the same thing, except one is public by default and the other is private.

By that I mean

struct Person
{
    string name;
};

// and 

class Person
{
    public:
        string name;
};


Are the same thing.

A mutator is just a member function that can change a underlying private variable of the class. Like this for example

class Person
{
    string name;

    public:
        void setName(string input) { name = input; }
}


In this class the setName function is a mutator. It is allowing the user of the class to change a private variable by using a function.

Accessors are just like mutators but reversed instead of changing the underlying variable they return that variables value.

These are also known as getters and setters.

Data abstraction is the hiding of implementation details from the user of the class by providing functions like seen above to manipulate that data. That way the class designer only has control over how that data is manipulated and the user doesn't. Doing that you're creating a interface which the user of the class uses (IE the public members) and the implementation of the class which the user doesn't (private members).

Here is a example

class Oven
{
    public:
        // Returns true if temp is less then or equal to 600
        // and false if it is not
        bool setTemp(int temp)
        {
            if (temp <= 600)
                ovenTemp = temp;
                return true;
            else
                return false
        }

    private:
        ovenTemp;
};


So lets say we are making a program for a oven. Would we want the user of that oven to be able to specify whatever temperature to heat up to? No we wouldn't because they would burn their house down. So we hide the variable that controls the temperature from the user and make a function that can make sure they only set the temperature below a certain point. This is a very very basic form of data abstraction and encapsulation.

The user has no idea how the program set the temp of the oven, and they don't need to all they need to know is that they need to call the setTemp() function to set that temp and that it can't be to high.

If you would like to know more about data abstraction and encapsulation you can look here or at other tutorials or post on this forum or others.

This post has been edited by Zereo: 04 May 2013 - 09:46 PM

Was This Post Helpful? 3
  • +
  • -

#5 irkzuptang  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 04-May 13

Re: countryType class

Posted 04 May 2013 - 01:56 AM

View PostZereo, on 04 May 2013 - 01:45 AM, said:

A struct and a class are the same thing, except one is public by default and the other is private.

By that I mean

struct Person
{
    string name;
};

// and 

class Person
{
    public:
        string person;
};


Are the same thing.

A mutator is just a member function that can change a underlying private variable of the class. Like this for example

class Person
{
    string name;

    public:
        void setName(string input) { name = input; }
}


In this class the setName function is a mutator. It is allowing the user of the class to change a private variable by using a function.

Accessors are just like mutators but reversed instead of changing the underlying variable they return that variables value.

Data abstraction is the hiding of implementation details from the user of the class by providing functions like seen above to manipulate that data. That way the class designer only has control over how that data is manipulated and the user doesn't.


Do i need to use data abstraction all the time? In the question I asked in the start of the thread, should I use data abstraction? And, why do they put ampersand in some functions and they also use const, what is that for?
void getTime(int& hours, int& minutes, int& seconds)const;

Was This Post Helpful? 0
  • +
  • -

#6 irkzuptang  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 04-May 13

Re: countryType class

Posted 04 May 2013 - 02:09 AM

And, how do default constructors work?
Was This Post Helpful? 0
  • +
  • -

#7 Zereo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 43
  • View blog
  • Posts: 108
  • Joined: 08-November 12

Re: countryType class

Posted 04 May 2013 - 02:13 AM

Quote

Do i need to use data abstraction all the time? In the question I asked in the start of the thread, should I use data abstraction?


No you don't need to use data abstraction all the time, though most of the time it is a good idea. As for the question of "do you need to use if for this assignment?", well that is for you to decide.

Quote

And, why do they put ampersand in some functions and they also use const, what is that for?


The ampersand is called the reference operator. When you use it in a member function or normal function it is telling the function not to create a copy of whatever variable you pass into that parameter. Instead it will just directly alter the variable. For example.

//Adds 10 to whatever number you pass in
void addTenToNumber(int &number)
{
    number += 10;
}

int main()
{
    int myNumber = 0;

    // myNumber now equals 10
    addTenToNumber(myNumber);

    // myNumber now equals 20
    addTenToNumber(myNumber);

    cout << myNumber << endl;
}


Notice how we don't return anything from the functions yet whatever number we pass to our function still changes. That is because we used the reference operator (&) which is telling the function that we want do our changes directly to that variable we pass in.

To learn why people use const on functions you can look here


Constructors are what we use to initialize all of our member variables to a default value when we create a object. The default constructor specifically is used when we make a call like this.

class Person
{
    public:
        // This is our default constructor
        Person() : name(""), age(0), favoriteNumber(0) {}

    private:
        string name;
        int age;
        int favoriteNumber;
};

int main()
{
    // This will call our default constructor to create the variable brandon
    Person brandon;
}


The point of the default constructor is to initialize all the variables in a class to a sensible default value. If you are only using class types like std::string you don't necessarily need a default constructor since the implicit constructor will work just fine. But if you have primitive types like int or double in your class you need to define a default constructor or some type of constructor otherwise them variables will be uninitialized when they are created and can lead to undefined behavior.

Now in my example I used initializer lists to define my default constructor (The : and the stuff after it) this is usually the best way to initialize your variables in your class, but for more complicated constructors or if you prefer not to use lists you can do it like this.

Person()
{
    name = "";
    age = 0;
    favoriteNumber = 0;
}


There is much more to know about constructors so I would recommend you read up on them with whatever resource you have to learn more about them if you are going to use them.

Remember make sure you have a good understanding on all these concepts before you move on. These are going to be used throughout programming and they are going to be used a lot so you need to know them.

This post has been edited by Zereo: 04 May 2013 - 02:27 AM

Was This Post Helpful? 4
  • +
  • -

#8 irkzuptang  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 04-May 13

Re: countryType class

Posted 04 May 2013 - 02:17 AM

View PostZereo, on 04 May 2013 - 02:13 AM, said:

Quote

Do i need to use data abstraction all the time? In the question I asked in the start of the thread, should I use data abstraction?


No you don't need to use data abstraction all the time, though most of the time it is a good idea. As for the question of "do you need to use if for this assignment?", well that is for you to decide.

Quote

And, why do they put ampersand in some functions and they also use const, what is that for?


The ampersand is called the reference operator. When you use it in a member function or normal function it is telling the function not to create a copy of whatever variable you pass into that parameter. Instead it will just directly alter the variable. For example.

//Adds 10 to whatever number you pass in
void addTenToNumber(int &number)
{
    number += 10;
}

int main()
{
    int myNumber = 0;

    // myNumber now equals 10
    addTenToNumber(myNumber);

    // myNumber now equals 20
    addTenToNumber(myNumber);

    cout << myNumber << endl;
}


Notice how we don't return anything from the functions yet whatever number we pass to our function still changes. That is because we used the reference operator (&) which is telling the function that we want do our changes directly to that variable we pass in.

To learn why people use const on functions you can look here


Thanks. How do classes inside a main class work? How do default constructors work? Sorry, I really don't have any idea in classes.
Was This Post Helpful? 0
  • +
  • -

#9 Zereo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 43
  • View blog
  • Posts: 108
  • Joined: 08-November 12

Re: countryType class

Posted 04 May 2013 - 02:32 AM

Quote

Thanks. How do classes inside a main class work


Not sure I understand what you are asking here. Could you be more specific?

Quote

How do default constructors work?


Edited my previous post to contain info on constructors.

Quote

Sorry, I really don't have any idea in classes.


No problem, classes and object oriented concepts are generally one of the hardest concepts for people learning C++ to grasp. Just keep sticking with it and look for as many resources on the subject as you can find (One might not make sense to you but the other might explain it perfectly).

This post has been edited by Zereo: 04 May 2013 - 02:33 AM

Was This Post Helpful? 0
  • +
  • -

#10 irkzuptang  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 04-May 13

Re: countryType class

Posted 04 May 2013 - 03:32 AM

View PostZereo, on 04 May 2013 - 02:32 AM, said:

Quote

Thanks. How do classes inside a main class work


Not sure I understand what you are asking here. Could you be more specific?


Sub-classes? Class within a class?

Quote

No problem, classes and object oriented concepts are generally one of the hardest concepts for people learning C++ to grasp. Just keep sticking with it and look for as many resources on the subject as you can find (One might not make sense to you but the other might explain it perfectly).


Thank you very much for helping me understand classes, although it'll really take me long time to grasp all the information regarding classes. Thanks! :)
Was This Post Helpful? 0
  • +
  • -

#11 pryious  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 06-May 13

Re: countryType class

Posted 06 May 2013 - 08:30 AM

Just wanted to say thank you zereo for helping me understand classes. Was having a problem just like irkz and yourexplanation helped me work through it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1