Upload
michael-gordon
View
234
Download
6
Embed Size (px)
DESCRIPTION
Citation preview
What are classes?
C++ was originally called “C with classes”
A class can be thought of as new type of
variable that you can create.
Just like functions are new operations you
can write, classes are new variables
We’ve used one kind of class already: the
string class
string and its functions are built into C++.
Strings
The internal construction of a string is
really an array of characters.
To find the length of a string s we can use
the functions .length() or .size
int x = s.length(); gives x the value of the
size of the string s.
To “concatenate” two strings s1 and s2:
string s3 = s1+s2;
String examples string first, last, name;
cout<<"Enter your first name:";
cin>>first;
cout<<"Enter your last name:";
cin>>last;
cout<<"Hi, "+first+" "+last; //concat
getline()
cin reads the typed string only until the first
whitespace.
If you want the whole string including
those whitespaces, you can use:
getline(cin, stringname);
cout<<“Enter your name:”;
getline(cin, fullname);
Use fullname instead of first and last.
“dot” functions
With strings we start dealing with a new
kind of function call. These functions
operate on a specific “object” (an
instance of a class).
We write the object name (e.g. the
variable name of the string) followed by
period and the function call (no space
before or after the period).