Using Standard Classes
Sample program using class string
// To use strings. #include <iostream> // Declaration of cin, cout #include <string> // Declaration of class string using namespace std; int main() { // Defines four strings: string prompt("What is your name: "), name, // An empty line( 40, '-'), // string with 40 '-' total = "Hello "; // is possible! cout << prompt; // Request for input. getline( cin, name); // Inputs a name in one line total = total + name; // Concatenates and // assigns strings. cout << line << endl // Outputs line and name << total << endl; cout << " Your name is " // Outputs length << name.length() << " characters long!" << endl; cout << line << endl; return 0; }
Note |
Both the operators + and += for concatenation and the relational operators <, <=, >, >=, ==, and != are defined for objects
of class string. Strings can be printed with cout and the operator <<.
The class
string will be introduced in detail later
on.
|
Sample screen output
What is your name: Rose Summer --------------------------------------- Hello Rose Summer Your name is 11 characters long! ---------------------------------------
Several classes are defined in the C++ standard library. These
include stream classes for input and output, but also classes for representing
strings or handling error conditions.
Each class is a type with certain properties and capacities. As
previously mentioned, the properties of a class are defined by its data members and the class's capacities
are defined by its methods. Methods are functions that
belong to a class and cooperate with the members to perform certain operations.
Methods are also referred to as member functions.
Creating Objects
An object is a variable of a class
type, also referred to as an instance of the class. When
an object is created, memory is allocated to the data members and initialized
with suitable values.
Example:
string s("I am a string");
In this example the object s, an instance
of the standard class string (or simply a string), is defined and initialized with the string constant
that follows. Objects of the string class manage the
memory space required for the string themselves.
In general, there are several ways of initializing an object
of a class. A string can thus be initialized with a certain number of identical
characters, as the example on the opposite page illustrates.
Calling Methods
All the methods defined as public
within the corresponding class can be called for an object. In contrast to
calling a global function, a method is always called for one
particular object. The name of the object precedes the method and is
separated from the method by a period.
Example:
s.length(); // object.method();
The method length() supplies the
length of a string, i.e. the number of characters in a string. This results in a
value of 13 for the string s defined above.
Classes and Global Functions
Globally defined functions exist for
some standard classes. These functions perform certain operations for objects
passed as arguments. The global function getline(), for
example, stores a line of keyboard input in a string.
Example:
getline(cin, s);
The keyboard input is terminated by pressing the return key
to create a new-line character, '\n', which is not
stored in the string.
2 Comments
cvvDo you need to hack into any, databaseserver spy on Facebook,Emails, Whatsapp, Viber, Snapchat, Instagram and many more.
ReplyDeleteI urge you to get in touch with the best people for the job, i have confirm the service when i need to spy on my spouse phone. They are good at Phone Cloning and Bitcoin/binary minning and any other hack job.
Thanks guys for the team work HACKINTECHNOLOGYATGMAILDOTCOM
+12132951376(WHATSAPP)
I was thrown out of my own house was sleeping in a hotel for weeks she also took possession of my son could only see him once a week then I found out she was in love with my accountant all these while so I went online and I came across a Russian private investigator who help me get all my properties and my accounts back even my company back how he did these I don’t know but I gave all the information he asked for and followed all his instructions and now I’m happy my life’s better now.
ReplyDeleteThanks to HACKINTECHNOLOGY@CYBERSERVICES.COM
I just said I should share my own story here
Thank you