From C to C++

Introduction

C++ began as "C with classes". Almost every C program is also a C++ program, so everything from the C course still works. This course is about what C++ adds, above all object-oriented programming: building programs out of objects that keep their data and the functions that work on it together.

It assumes the C course up to structs and malloc.

C++ keeps everything you know from C and adds new ways of working. This first lesson changes two everyday tasks, input and output and text, and in doing so introduces the idea of an object.

How it works

Input and output with streams

#include <iostream>
using namespace std;

int main() {
    int age;
    cout << "How old are you? ";
    cin >> age;
    cout << "Next year you will be " << age + 1 << "." << endl;
    bool adult = age >= 18;
    cout << "adult: " << adult << endl;
    return 0;
}

Input used: 19

Open in compiler
  • #include <iostream> brings in the stream library. cout is the output stream and cin the input stream.
  • << sends values to cout, one after another, and >> reads from cin into a variable. There are no format codes like %d: the type of the variable decides how it is read and printed.
  • endl ends the line (like "\n").
  • using namespace std; lets you write cout instead of std::cout. The standard library's names live in the namespace std.

C++ also has a real bool type with the values true and false. cout prints them as 1 and 0.

Syntax

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

string name;
cin >> name;                      // read one word
cout << "Hi, " << name << endl;   // print several values
cout << name.length() << endl;    // call a member function

Example

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

int main() {
    string first = "Nadia";
    string last = "Rahman";
    string full = first + " " + last;
    cout << full << " has " << full.length() << " characters" << endl;
    if (first == "Nadia") cout << "Hi, " << first << "!" << endl;
    full += " (CSE)";
    cout << full << endl;
    cout << "First letter: " << full[0] << endl;
    return 0;
}
Open in compiler

A C string is a char array that you must size and copy with strcpy. A C++ string is an object that manages its own memory: + joins strings, == compares them, += appends, and length() gives the size. In the visualizer a string shows its text; the characters live in a heap block it allocates and frees by itself.

full.length() is a member function call: a function that belongs to the object full. Writing your own classes with member functions is what the rest of this course is about.

Line by line

  1. #include <string> brings in the string class, and using namespace std; lets the code write string instead of std::string.
  2. first and last are string objects, set from text in quotes.
  3. first + " " + last joins three pieces into a new string, with no buffer to size and no strcpy.
  4. full.length() asks the object full for its number of characters, which is 12, counting the space.
  5. first == "Nadia" compares the text itself, so the greeting is printed.
  6. full += " (CSE)" appends to full, and the string makes room for the extra text by itself.
  7. full[0] reads the first character, just as with a C array.

Expected result

Nadia Rahman has 12 characters
Hi, Nadia!
Nadia Rahman (CSE)
First letter: N

In the real world

Almost every C++ program uses streams and string. Text-heavy software such as web servers, compilers and editors depends on strings that manage their own memory, because hand-sized char buffers are a classic source of security bugs.

Common mistakes

  • cin >> name reads one word only: it stops at a space. To read a whole line, use getline(cin, name).
  • Comparing C strings (char *) with == compares addresses; comparing string objects with == compares text.
  • Forgetting #include <string>: some compilers still work through <iostream>, but do not rely on it.

Best practices

  • Use string instead of char arrays for text in C++.
  • Read whole lines with getline when the input may contain spaces.
  • Include the header for everything you use, and do not rely on one header including another.

Summary

C++ builds on C. Streams read and print values without format codes, bool is a real type, and string is an object that stores and manages text, with member functions such as length().

Quick check

1. What does cout << 3 << 4; print?
2. Which reads a whole line, spaces included?
3. string a = "ab"; string b = a + "cd"; What is b.length()?

Key takeaways

  • Streams choose the format from each variable's type.
  • A string manages its own memory, so it can grow and be copied safely.
  • full.length() is a member function call on the object full.

Practice problems for this lesson

My notes

Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.