C++: A string class and its method

This entry is part 9 of 61 in the series C++

In our previous post, we have mentioned about string as a class.  In our later posts, we will talk more about Object Oriented programming and its core concepts.

The idea about object oriented programming is that you can create objects and define their relevant characteristics.  This could sound a bit abstract when you first started learning OOP.

Since we say string is a class, we should be able to create an object out of it.  Let’s look at this example.

#include <iostream>
#include <string>
using namespace std;

void main()
	string str = "Stay calm and keep learning";

	int length = str.size();

	cout << "The length of the string is: " << length;

	cout << endl;  //Output end of line character


In this example, we have defined an instance of a class string and this instance hold a string of character.

Making use of the string class and its relevant characteristics, we can find the length of the string str easily uisng str.size().

In later lessons, we will learn that size() is a method of the string class.  And we are using a dot(.) operator to assess its method called size().


Series Navigation<< C++: The string class
C++: Flow control with if statement >>