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

In Java I know how it does, but in C / C++ it's little different so 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 one .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 the attributes and methods and also where do I create the constructor?

Author: Maniero, 2016-03-13

1 answers

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

The declaration of the data structure is usually done separately from the implementation of the methods (it is possible to do the implementation in the declaration itself, but there are disadvantages to doing this (there are advantages too, so it depends on the case to choose one or the other).

In general the declaration is placed in files header and the implementation in .cpp. But it doesn't have to be like that. Again it has advantages and disadvantages in each. In Java the declaration and implementation is one thing.

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

Methods are declared together from the class declaration as well. In some cases it is possible to have your implementation inline. That gives some flexibility but it exposes the source (headers with declarations are always needed in the build to consume a class in some code) and requires a compilation of the code every time it is used (simply that's it).

To avoid the above disadvantages and eventually obtain other characteristics it is very common for the implementation of the methods to be separated. That's what's in .cpp.

The constructor is no different, it is also declared during the declaration .h or .hpp) and the implementation can be right there or, most commonly, in .cpp. In the example everything is already right, just write his body, if you need a constructor . The destroyer probably needs even less.

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

Each member must be placed in the block according to the visibility that it must 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;
}

I put on GitHub for future reference .

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

 3
Author: Maniero, 2019-12-10 18:13:48