Consider a simple c++ porgram like this below
#include<iostream>
// Base class
class Vehicle {
public:
string brand = "Ford";
void honk() {
cout << "Tuut, tuut! \n" ;
}
};
// Derived class
class Car: public Vehicle {
public:
string model = "Mustang";
};
int main() {
Car myCar;
myCar.honk();
cout << myCar.brand + " " + myCar.model;
return 0;
}
This is plain old method C++14,C++17 uses new syntax methods, t get familiarize with those you can use clang-tidy tool.
Install clang-tidy tool
sudo apt-get install clang-tidy
use the below command to get tips on how to modernize the code
clang-tidy --checks='modernize*, readability*' simple.cpp -- -std=c++17
It will give information like this
7173 warnings and 4 errors generated.
Error while processing /home/gharshan/Desktop/simple.cpp.
/home/gharshan/Desktop/simple.cpp:6:5: error: unknown type name 'string'; did you mean 'std::string'? [clang-diagnostic-error]
string brand = "Ford";
^~~~~~
std::string
/usr/lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stringfwd.h:79:33: note: 'std::string' declared here
typedef basic_string<char> string;
^
/home/gharshan/Desktop/simple.cpp:7:10: warning: method 'honk' can be made static [readability-convert-member-functions-to-static]
void honk() {
^
static
/home/gharshan/Desktop/simple.cpp:8:7: error: use of undeclared identifier 'cout'; did you mean 'std::cout'? [clang-diagnostic-error]
cout << "Tuut, tuut! \n" ;
^~~~
std::cout
/usr/lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/iostream:61:18: note: 'std::cout' declared here
extern ostream cout; /// Linked to standard output
^
/home/gharshan/Desktop/simple.cpp:15:5: error: unknown type name 'string'; did you mean 'std::string'? [clang-diagnostic-error]
string model = "Mustang";
^~~~~~
std::string
/usr/lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stringfwd.h:79:33: note: 'std::string' declared here
typedef basic_string<char> string;
^
/home/gharshan/Desktop/simple.cpp:18:5: warning: use a trailing return type for this function [modernize-use-trailing-return-type]
int main() {
~~~ ^
auto -> int
/home/gharshan/Desktop/simple.cpp:21:3: error: use of undeclared identifier 'cout'; did you mean 'std::cout'? [clang-diagnostic-error]
cout << myCar.brand + " " + myCar.model;
^~~~
std::cout
/usr/lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/iostream:61:18: note: 'std::cout' declared here
extern ostream cout; /// Linked to standard output
^
Suppressed 7171 warnings (7171 in non-user code).
Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.
Found compiler error(s).