How to create a class with attributes and methods in C ++?

3

In Java I know how to do it, but in C / C ++ it's little different then how to create a class with C ++ attributes and methods?

I'm using CodeBlocks.

When I create a class in C ++ it creates two files a .h and another .cpp

  • File.h

    #ifndef TETES_H
    #define TETES_H
    
    
    class Tetes
    {
        public:
            Tetes();
            virtual ~Tetes();
        protected:
        private:
    };
    
    #endif // TETES_H
    
  • File.cpp

    #include "Tetes.h"
    
    Tetes::Tetes()
    {
        //ctor
    }
    
    Tetes::~Tetes()
    {
        //dtor
    }
    

Where will I define attributes and methods, and also where do I build the constructor?

    
asked by anonymous 13.03.2016 / 21:54

1 answer

3

The example in question (after editing) indicates a fundamental difference between Java and C ++ (C does not allow classes).

The declaration of the data structure is usually made separate from the implementation of the methods (you can do the implementation in the declaration itself, but there are disadvantages in doing this (there are advantages too, so it depends on the case to choose one or the other) / p>

In general, the statement is placed in header and implementation in .cpp . But do not need be like that. Again it has advantages and disadvantages in each. In Java the declaration and implementation is one thing.

The attributes are always placed in the declaration. The example below with comments gives an idea of the difference of the declaration and the implementation. After all attributes are only part of the data structure.

The methods are declared together with the class declaration as well. In some cases it is possible to have your inline implementation. This gives some flexibility but exposes the source (headers with declarations are always required in compiling to consume a class in some code) and requires a code compilation every time it is used (simplified is that).

To avoid the disadvantages above and eventually to obtain other characteristics it is very common that the implementation of the methods are separated. It is what is in .cpp .

The constructor is not different, it is also declared during the class declaration (usually .h or .hpp ), and the implementation may be there or, most commonly, .cpp . In the example everything is all right, all you need to do is write his body, if you need a builder . The destructor probably needs less.

Remember that in C ++, just like C, method or function signature is different of its implementation.

Each member should be placed in the block according to the visibility it should have.

A simple example taken from this source :

#include <iostream> //carrega um arquivo de definições (semelhante mas diferente do import)
using namespace std; //permite acessar os membros deste "pacote" diretamente

class Rectangle {
    int width, height; //são privados por default
  public: //tudo abaixo é público
    Rectangle(int, int); //note só a assinatura do construtor (poderia ser inline também)
    int area() { return width * height; }//implementação inline; pode escolher o + indicado
}; //declaração tem ; em alguns casos ela fica melhor em um header .hpp

Rectangle::Rectangle(int a, int b) { //implementação do construtor separado da declaração
    width = a;
    height = b;
}

int main () { //essa parte é só para testar
  Rectangle rect (3,4); //instanciação, tem outras formas de fazer o mesmo
  Rectangle rectb (5,6);
  cout << "rect area: " << rect.area() << endl;
  cout << "rectb area: " << rectb.area() << endl;
  return 0;
}

In addition to slightly different syntax, the semantics of C ++ classes are significantly different from Java.

    
13.03.2016 / 22:07