• Martin Thoma
  • Home
  • Categories
  • Tags
  • Archives
  • Support me

Structs in C++

Contents

  • Structs in C++
    • Point
      • Basic example
      • Functions
    • More stuff
      • Initialization
      • Constructors
      • Functions in structs
    • Read also

I guess this article isn't very interesting, except if you have VERY little experience with C / C++. I only give some complete code examples. If you want some text, you could read C++ Programming/Structures (a wikibook).

Point

Basic example

#include <iostream>

using namespace std;

struct Point {
    double x;
    double y;
};

int main() {
    Point a = {12, 34};
    printf("(%.2f|%.2f)\n", a.x, a.y);
    return 0;
}

Functions

#include <iostream>
#define ABS(a) (a < 0 ? -(a) : a)

using namespace std;

struct Point {
    double x;
    double y;
};

double getManhattanDistance(Point a, Point b) {
    return ABS(a.x-b.x) + ABS(a.y-b.y);
}

int main() {
    Point a = {1, 1};
    Point b = {3, -4};
    printf("(%.2f|%.2f)\n", a.x, a.y);
    printf("(%.2f|%.2f)\n", b.x, b.y);
    printf("Distance: %.2f\n", getManhattanDistance(a, b));
    return 0;
}

More stuff

Initialization

Point a = {} initializes all values of point to 0.

Constructors

You can write constructors for structs:

#include <iostream>
#define ABS(a) (a < 0 ? -(a) : a)

using namespace std;

struct Point {
    double x;
    double y;

    Point():x(2.0),y(5.0) {}
    Point(double a, double b):x(9.0),y(9.0) {x=a; y=b;}
};

double getManhattanDistance(Point a, Point b) {
    return ABS(a.x-b.x) + ABS(a.y-b.y);
}

int main() {
    Point a = {};
    Point b = {3, -4};
    Point c;
    printf("(%.2f|%.2f)\n", a.x, a.y);
    printf("(%.2f|%.2f)\n", b.x, b.y);
    printf("(%.2f|%.2f)\n", c.x, c.y);
    printf("Distance: %.2f\n", getManhattanDistance(a, b));
    return 0;
}

Which gives:

./struct-example.out
(2.00|5.00)
(3.00|-4.00)
(2.00|5.00)
Distance: 10.00

Functions in structs

You can also add functions to structs:

#include <iostream>
#define ABS(a) (a < 0 ? -(a) : a)

using namespace std;

struct Point {
    double x;
    double y;

    Point():x(2.0),y(5.0) {}
    Point(double a, double b):x(9.0),y(9.0) {x=a; y=b;}

    double getZeroDist() {
        return ABS(x)+ABS(y);
    }
};

double getManhattanDistance(Point a, Point b) {
    return ABS(a.x-b.x) + ABS(a.y-b.y);
}

int main() {
    Point a = {3, -10};
    printf("(%.2f|%.2f)\n", a.x, a.y);
    printf("Distance: %.2f\n", a.getZeroDist());
    return 0;
}

Result:

./struct-example.out
(3.00|-10.00)
Distance: 13.00

Read also

  • Wikipedia: struct (C programming language)
  • Operator overloading
  • A practical approach to floats: An example for union
  • Connect four: One usage example for structs

Is there anything interesting to say about structs?


Published

Jun 7, 2013
by Martin Thoma

Category

Code

Tags

  • CPP 10
  • struct 1

Contact

  • Martin Thoma - A blog about Code, the Web and Cyberculture
  • E-mail subscription
  • RSS-Feed
  • Privacy/Datenschutzerklärung
  • Impressum
  • Powered by Pelican. Theme: Elegant by Talha Mansoor