Create classes and functions in c++

Asked

Viewed 403 times

-3

One can give an example of how to create the main, the . h, and the . cpp in c++? and also connect them all. For example, having a function that receives an input value a, having another class that receives input a b and a separate class do the sum and return to main. I’m new to this language.

  • 2

    Is there any way to put in the question the code you tried to do? This way we can better see which parts you are struggling with. I would add that a "I don’t know how to start" response is a good sign that you should review the basics before trying something like what you’re doing. Maybe review the contents of building algorithms and re-read the material on C syntax++.

1 answer

0


Include default for language use

#include <iostream>

using standard for language use

using namespace std;

Creating a class Acampamentos

class Acampamentos{
public:
    int idade;
    string nome;
    char equipe;

    Acampamentos();
    void imprimir();
    void separarGrupo();
};

Default format of the constructor

Acampamentos::Acampamentos(){

    idade =0;
    nome = " ";
    equipe = ' ';
}

Creating a function imprimir() only to print class attributes on the console

void Acampamentos::imprimir(){
    cout << "Idade .: " << idade << endl;
    cout << "Nome ..: " << nome << endl;
    cout << "Equipe : " << equipe << endl;
}

Way to print on console
cout << "String desejada" << Concatenate with the desired attribute << endl
Ndle skips line at the end of writing on the console (End Line)

Creating a function seperarGrupo where sets the team by age

void Acampamentos::separarGrupo(){

    if(idade >=6 && idade <= 10){
        equipe = 'A';
    }

    if(idade > 10 && idade <= 20){
        equipe = 'B';
    }

    if(idade >20){
        equipe = 'C';
    }
}

Creating the main

int main(int argc, char** argv) {
    Acampamentos A;

    cout << "Escreva o nome : ";
    cin >> A.nome;
    cout << endl;
    cout << "Digite a idade :";
    cin >> A.idade;
    cout << endl;
    system("cls");
    A.imprimir();
    system("cls");
    A.separarGrupo();
    A.imprimir();
    return 0;
}

To receive an input and assign to another variable cin >> Atributo desejado . If you are in a class, use the instance Instancia.atributo_desejado

Browser other questions tagged

You are not signed in. Login or sign up in order to post.