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
#include <iostream>brings in the stream library.coutis the output stream andcinthe input stream.<<sends values tocout, one after another, and>>reads fromcininto a variable. There are no format codes like%d: the type of the variable decides how it is read and printed.endlends the line (like"\n").using namespace std;lets you writecoutinstead ofstd::cout. The standard library's names live in the namespacestd.
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;
}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
#include <string>brings in thestringclass, andusing namespace std;lets the code writestringinstead ofstd::string.firstandlastarestringobjects, set from text in quotes.first + " " + lastjoins three pieces into a new string, with no buffer to size and nostrcpy.full.length()asks the objectfullfor its number of characters, which is 12, counting the space.first == "Nadia"compares the text itself, so the greeting is printed.full += " (CSE)"appends tofull, and the string makes room for the extra text by itself.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 >> namereads one word only: it stops at a space. To read a whole line, usegetline(cin, name).- Comparing C strings (
char *) with==compares addresses; comparingstringobjects with==compares text. - Forgetting
#include <string>: some compilers still work through<iostream>, but do not rely on it.
Best practices
- Use
stringinstead ofchararrays for text in C++. - Read whole lines with
getlinewhen 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
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.