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

Asked

Viewed 9,873 times

3

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

I am 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
    
  • cpp file.

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

Where I will define the attributes and methods and also where I create the constructor?

1 answer

3


The example posed in the 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 (it is possible to implement it in the statement itself, but there are disadvantages in 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 archives header and implementation in .cpp. But it doesn’t have to be so. Again it has advantages and disadvantages in each one. 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 in the declaration and implementation. After all attributes are only part of the data structure.

Methods are declared class declaration joint as well. In some cases it is possible to have their implementation inline. This gives some flexibility but exposes the source (the headers with the statements 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 get other characteristics it is very common that the implementation of the methods are separated. It’s what’s in the .cpp.

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

Remember that a in C++, as well as C, a signature of the method or function is different from its implementation.

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

A simple example 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 in the Github for future reference.

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

  • 1

    what that part would be Tetes::~Tetes()?

  • 1

    It is the destructor. It’s another language, it works a little different, but it’s similar: http://answall.com/q/75613/101, http://answall.com/q/9078/101

  • 1

    @Maniero Where the interface enters?

  • @Khyser nowhere, the question is not about this.

  • @Maniero More than audacity

Browser other questions tagged

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