C++
| Introduction | |
| using namespace std; | |
| Guess the number | |
| Overloading functions | |
| Return Type | |
| Multiple Files | |
| Header Files | |
| Strings | |
| getline() | |
| Errors |
Introduction
This is the second part of the article on C++ the previous part here next here
using namespace std;
A beginner to learn C++ may wonder why.
A lot of code is written before main().
For example, why write? using namespace std;
You can not write, but then with each use of cout or, for example,
The endl will need to specify from which namespace
You take them.
It will look something like this:
#include <iostream>
int main()
{
std::cout << "Visit HeiHei.ru!" << std::endl << "Or visit TopBicycle.ru" << std::endl << 2.5*4;
return 0;
}
If you use multiple libraries at once, it is probably even better – there is no risk.
Get a name conflict.
But for a beginner to learn C++ it may be too cumbersome.
using namespace Std; allows you to write the same without constant std:
#include <iostream>
using namespace std;
int main()
{
cout << "Visit HeiHei.ru!" << endl << "Or visit TopBicycle.ru" << endl << 2.5*4;
return 0;
}
Guess the number
Exercise: Write a game in which the user must guess a predetermined number.
It is necessary to give clues whether the entered number is greater or less than the predicted number.
Decision:
#include <iostream>
using namespace std;
int main() {
int i;
bool keepgoing = true;
int answer = 7;
while (keepgoing)
{
cout << "Guess the number ";
cin >> i;
if (i < answer)
{
cout << "Your number, " << i << ", is less than the hidden number. Try again" << endl;
}
if (i == answer)
{
cout << "You guessed it, congratulations, game over" << endl;
keepgoing = false;
}
if (answer < i)
{
cout << "Your number, " << i << ", is greater than the hidden number. Try again" << endl;
}
if (i != answer) {
cout << "Try again? 0 for no ";
cin >> again;
if (again == 0)
{
keepgoing = false;
}
}
}
}
An example of a function that adds two numbers and does not return warnings type-error int - double
#include <iostream>
using namespace std;
double add(double x, double y)
{
return x + y;
}
int main() {
auto a = add(3, 4);
cout << "3 + 4 is " << a;
// 7
cout << endl;
double b = add(1.3, 4.5);
cout << "1.3 + 4.5 is " << b;
// 5.8
return 0;
}
Overloading functions
Two different functions may have the same name. This can be done if you have different arguments.
Such overloading is considered good practice.
An example of add overload is another add function, but for example, with three parameters.
double add(double a, double b, double c)
{
return a + b + c;
}
You can overload a function with the same number of arguments if the arguments have different types. This is considered bad practice.
Overloading by the type of returned value cannot be done.
#include <iostream>
using namespace std;
bool test(bool x)
{
return x;
}
bool test(double x)
{
return x > 0;
}
int main() {
// 1
if (test(true))
{
cout << "true passes the test" << endl;
}
// 2
if (test(3.2))
{
cout << "3.2 passes the test" << endl;
}
// 3
if (test(3))
{
cout << "3.2 passes the test" << endl;
}
return 0;
}
1 and 2 can exist together. But as soon as you want to check. Integer, you get a mistake.
1>C:\Users\eth1.ru\source\repos\project\example.cpp(55,5): error C2668: 'test': ambiguous call to overloaded function
1>C:\Users\eth1.ru\source\repos\project\example.cpp(20,6): message : could be 'bool test(double)'
1>C:\Users\eth1.ru\source\repos\project\example.cpp(15,6): message : or 'bool test(bool)'
Return Type
As you can see, most functions end with return 0;
If you write a function that should not return anything, you need to specify the name of the function void.
#include <iostream>
using namespace std;
void test()
{
return;
}
Multiple Files
The file can be divided into several parts. In the place where you call main() specify the names
The functions you have transferred to another file and the arguments they accept.
Main file:
#include <iostream>
using namespace std;
double add(double x, double y);
double add(double a, double b, double c);
bool test(bool x);
bool test(double x);
void test(int y);
int main() {
and so on
File with functions:
bool test(bool x)
{
return x;
}
bool test(double x)
{
return x > 0;
}
double add(double x, double y)
{
return x + y;
}
double add(double a, double b, double c)
{
return a + b + c;
}
Header Files
To not list all the features you want to use in the main file you can mention them
in a special Header file with the extension .h
This file will then need to be connected to the main file using the #include directive.
Main file:
#include <iostream>
#include "Functions.h"
Functions file. h h
double add(double x, double y);
double add(double a, double b, double c);
bool test(bool x);
bool test(double x);
void test(int y);
The Functions.cpp file remains unchanged.
Strings
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "who are you?" << endl;
cin >> name;
string greeting = "Hello, " + name;
if (name == "Andrei")
{
greeting += ", I know you!";
}
cout << greeting << endl;
int l = greeting.length();
cout << "\"" + greeting + "\" is " << l << " characters long. " << endl;
auto space = greeting.find(' '
string beginning = greeting.substr(space + 1);
cout << beginning << endl;
if (beginning == name)
{
cout << "expected result. " << endl;
}
return 0;
}
The task is to compare the two words entered and determine which is longer.
using namespace std;
int main()
{
string word1;
string word2;
cout << "Enter first word" << endl;
cin >> word1;
cout << "Enter 2nd word" << endl;
cin >> word2;
if (word1.length() == word2.length())
{
cout << "words have the same length";
}
if (word1.length() > word2.length())
{
cout << "word1 is longer";
}
if (word1.length() < word2.length())
{
cout << "word2 is longer";
}
return 0;
}
This code will only work if the word is entered without spaces.
To compare line lengths, use getline()
#include
#include
using namespace std;
int main()
{
string line1;
string line2;
cout << "Enter first word" << endl;
getline(cin , line1);
cout << "Enter 2nd word" << endl;
getline(cin , line2);
if (line1.length() == line2.length())
{
cout << "words have the same length";
}
if (line1.length() > line2.length())
{
cout << "line1 is longer";
}
if (line1.length() < line2.length())
{
cout << "line2 is longer";
}
return 0;
}
Errors
If it starts with C, it is a compiler error.
If it's an L, it's a battleship.
test local function definitions are illegal C++
It appears if you accidentally identified a function not before the main, but inside the main.
Error C2660 : function does not take 3 arguments
The compiler expects the function to have a different number of arguments.
It appears, for example, if you do not connect the desired function, but exists.
Overload function with a different number of arguments. Copyler can't find
the desired function, finds the one with the same name, tries to use,
But the number of arguments is wrong.
Error LNK2019 unresolved external symbol
Linker sees that a function is called that is not specified anywhere.
Article author: Andrei Olegovich