DEV Community

Cover image for Intro to C++
gfish94
gfish94

Posted on

Intro to C++

C++ is a general purpose, object oriented programming language that developed as an extension of C by Bjarne Stroustrup. C++ was originally called "C with classes". C++ was given its official name in 1983 and was commercially released in 1985.

Variables

Variables in C++ have to be declared with the datatype that they are intended to store. For numbers, int is used to store whole numbers and double is used to store floating point numbers. char is used to store single characters and the value must be wrapped in single-quotes. string is used to store text strings and the value must be wrapped in double-quotes. bool is used to store boolean values which output to 1 and 0 respectively.


/*
multiple variables of the same type can
be initialized on the same line
*/
int x = 1, y = 2, z = 3;

//const creates a read only variable
const int sum = x + y + z;

double decimal = 0.1;

char letter = 'A';

string sentence = "This is a sentence.\n";

/*
true => 1
false => 0
*/
bool t = true, f = false;

Enter fullscreen mode Exit fullscreen mode

C++ variable meme

Functions

In C++, functions are declared using the datatype that the function is meant to return or void if there is no return. Parameters must also be declared with the datatype that are intended to be passed as arguments. When possible the datatype will be converted to the appropriate datatype, such as with int and double. Conversion will mutate the value and the amount of storage it takes in memory. In the case of double to int the value will be rounded up to the nearest whole number value.

#include <iostream>
using namespace std;

int add(int x, int y)
{
  return x + y;
};

double add(double x, double y)
{
  return x + y;
};

void print(double x, double y)
{
  string result = (x < y)
                      ? "x is less than y\n"
                      : "x is greater than y\n";
  cout << result;
};

int main()
{

  int x1 = add(1, 2);
  int y1 = add(3, 4);
  //will convert int to double when necessary
  print(x1, y1);

  double x2 = add(11.2, 5.5);
  double y2 = add(10.4, 5.3);
  print(x2, y2);


  return 0;
}

Enter fullscreen mode Exit fullscreen mode

Classes and Objects

Living up to its original name, C++ is syntactically similar to its predecessor but with the additional support for classes and objects. Classes are created using class followed by curly braces containing the members -- props and methods. Classes require an access specification or it will default to private which makes the members inaccessible or viewable outside of the class. public allows members to be accessed outside of the class. protected works similarly to private, however it allows the members to be accessed by derived classes.

#include <iostream>
using namespace std;

class Pet {
  //access level
  public:
  string name;
  string species;
  int age;

  //constructor
  Pet(string x, string y, int z){
    name = x;
    species = y;
    age = z;
  }


  // method
  void print()
  {
    cout << this->name
         << ' ' << this->species
         << ' ' << this->age << endl;
  }
};

int main(){
  //creates a new Pet object
  Pet obj("Bobby", "cat", 1);

  //method call
  obj.print();

  return 0;
}
Enter fullscreen mode Exit fullscreen mode

Morpheus saying c++ is going strong

Conclusion

C++ is a widely used language due to its fast and efficient nature. Many things are made using C++ including OS, game engines, databases, web browsers, machine learning, and telecommunications just to name a few. In other words, the modern world runs on C++.

Top comments (0)