C++ Keywords You Should Know (2024)

C++ has various keywords, and you should know what they are and how to use them. So in this article, I will be talking about some of the most important keywords you'll find in the language.

What are Keywords in C++?

Keywords are certain reserved words which have a predefined meaning in C++. Since keywords have their own predefined meaning, they cannot be used as identifiers (for example, a function's name or a variable's name).

The function definition below is an error because friend is a keyword in c++.

void friend(){/*.......*/}

Let's look at some of the common keywords in C++ and how they're used.

C++ Keywords

The typedef keyword in C++

Sometime it can be tedious to declare a variable of a certain type like this:

const unsigned char

Programmers have hard time declaring such variables, and they're too long to use frequently. Can't we make it shorter or create something different? Yes we can.

Using typedef we can create synonyms:

typedef const unsigned char CON_UCHAR;

So instead of using such long base types, like this:

const unsigned char x;

We can use this:

CON_UCHAR x;

typedefs are helpful for pointer declaration, too:

typedef char *const CON_PTRCHAR; //const pointer to chartypedef const char* PTR_CONCHAR; //pointer to const char

The bool keyword in C++

bool is a type name which has two values – it is either true or false.

Every non-zero value is true, while zero is false.

if(1){std::cout<<"Hello World"<<'\n';}else{std::cout<<"Sorry World<<'\n';}

Since all non zero values are true, every time the program runs, it outputs Hello World.

Remember that the two bool values, true and false, are also keywords.

The using keyword in C++

using namespace std;

You might have used the using keyword unknowingly. It can be used like this:

using-declaration:

namespace My_space{class My_class{/*Your code here*/};}namespace Her_space{using My_space:: My_class;}

This is what a using declaration looks like. A using declaration brings every declaration with a given name to a scope.

using directive:

The most common example of a using directive is this:

using namespace std;

The above line of code makes every name from std namespace available.

The public, protected, and private keywords in C++

The keywords public, protected, and private are used as access specifiers in a class.

class Home{private:int members;protected:double tot_expenditure;public:void display_detail();};

The members after the private label are only accessible through member functions. If no label is provided, then it is private by default.

The members after the public label are accessible everywhere.

The members after the protected label are similar to public members for derived class and are similar to private members for non-derived class.

The enum keyword in C++

An enumeration is a user defined type. We declare enumeration using the enum keyword.

enum days{SUN, MON, TUE, WED, THU, FRI, SAT};

Here, SUN, MON, TUE ... are called enumerators and their values are assigned increasing from 0.

By default, SUN==0, MON ==1 and so on.

However, we can initialize enumerators ourselves too.

enum{ a = 5, b = 6};

By default, enumerations are converted to integers for arithmetic operations. Since enums are user defined types, we can overload operators for them, too.

The new keyword in C++

The new keyword (also a operator) is used to create objects in free store (also referred to as heap).

int main(){int *p = new int;*p = 20;//..}

For the above line of code, the new operator allocates a memory for storing an object of integer type and returns a pointer pointing to that allocated address.

The delete keyword in C++

Memory is an important resource for us. So we should use it wisely. Unwanted memory should not be occupied. So delete (also a operator) is used to deallocate memory that was previously allocated by the new operator.

int main(){int *p = new int;*p =20;delete p;//..}

The this keyword in C++

All the (non-static) member functions know for which object they were invoked and they can refer to it using the pointer this.

class A{int b;public://...void display() const;};

Consider a class which has a private member b and a member function display.

Our display function would be the following:

void A::display() const{cout<<"b = "<<b<<'\n';}

The same code above can be written using this like so:

void A::display() const{cout<<"b = "<<this->b<<'\n';}

Note that this is a pointer, so we should use the -> operator. Furthermore this here refers to the object which invoked the function.

The class keyword in C++

C++ supports object oriented programming, so we have the concept of classes. The keyword class is used to declare / define a class.

class Fb_user{//..your code here};

struct is also a keyword. It's a class with all the members labelled public by default.

struct Fb_user{//...Your code here};

The above code is shorthand for this code:

class Fb_user{public://..your code here};

To learn more about classes you can refer to my in-depth article about classes here.

The operator keyword in C++

The keyword operator is used while overloading operators. The syntax for overloading an operator is the following:

return_type operator operator's_symbol(parameters){//...Your code here...}

To learn more about overloading operators in c++ you can refer to my article here.

The inline keyword in C++

The keyword inline is used with functions which are expanded " in line " during every call.

A member function defined within the class definition is taken to be an inline function.

But we can use the keyword inline to inline a member function like this:

class Random(){int a;public:int display const();//...};inline int Random::display() const{return a;}

The 'inline' specification is just a request to the compiler to inline a function. The compiler may ignore that request.

The goto keyword in C++

C++ also supports the goto keyword. goto is used as a jump statement to jump in and out of a block. The restriction is that we cannot jump into an exception handler.

goto statements are useful for breaking out from a nested loop or any switch case statement.

int main(){for(int i = 0 ; i < 5 ; i++){ for(int j = 0 ; j < 5 ; j++{ if(){//check for some condition goto here; } }}here:cout<<"I am here"<<;}

So basically we use goto to jump from one block to another.

It is a good idea to avoid using goto in general, though it can sometimes be useful.

That's It!

These are some of the most common keywords in C++. I hope you had a good time reading about this (not the this pointer :) )

Happy Coding!

You can read my other blogs here.

C++ Keywords You Should Know (2024)

References

Top Articles
John Wick: Chapter 4 release date and everything we know so far
Fifty Shades Freed | Rotten Tomatoes
Kansas City Kansas Public Schools Educational Audiology Externship in Kansas City, KS for KCK public Schools
Cottonwood Vet Ottawa Ks
Fully Enclosed IP20 Interface Modules To Ensure Safety In Industrial Environment
Txtvrfy Sheridan Wy
Steamy Afternoon With Handsome Fernando
Best Transmission Service Margate
Teamexpress Login
Kris Carolla Obituary
Horned Stone Skull Cozy Grove
Bill Devane Obituary
4156303136
Knaben Pirate Download
C-Date im Test 2023 – Kosten, Erfahrungen & Funktionsweise
Chile Crunch Original
Arboristsite Forum Chainsaw
What Happened To Anna Citron Lansky
New Stores Coming To Canton Ohio 2022
Gdp E124
Wal-Mart 140 Supercenter Products
Commodore Beach Club Live Cam
Air Force Chief Results
Craigslist Prescott Az Free Stuff
Teekay Vop
Project Reeducation Gamcore
How To Tighten Lug Nuts Properly (Torque Specs) | TireGrades
Effingham Daily News Police Report
Vht Shortener
Cylinder Head Bolt Torque Values
Mcclendon's Near Me
Dell 22 FHD-Computermonitor – E2222H | Dell Deutschland
Spirited Showtimes Near Marcus Twin Creek Cinema
Craigslist Central Il
Steven Batash Md Pc Photos
Aliciabibs
Yogu Cheshire
Craigslist Pets Plattsburgh Ny
How to Print Tables in R with Examples Using table()
Sun Tracker Pontoon Wiring Diagram
Lamont Mortuary Globe Az
ACTUALIZACIÓN #8.1.0 DE BATTLEFIELD 2042
VDJdb in 2019: database extension, new analysis infrastructure and a T-cell receptor motif compendium
St Vrain Schoology
Wolf Of Wallstreet 123 Movies
8 4 Study Guide And Intervention Trigonometry
Cara Corcione Obituary
Haunted Mansion Showtimes Near Millstone 14
Star Sessions Snapcamz
Guy Ritchie's The Covenant Showtimes Near Look Cinemas Redlands
Duffield Regional Jail Mugshots 2023
All Obituaries | Roberts Funeral Home | Logan OH funeral home and cremation
Latest Posts
Article information

Author: Rev. Leonie Wyman

Last Updated:

Views: 5448

Rating: 4.9 / 5 (79 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Rev. Leonie Wyman

Birthday: 1993-07-01

Address: Suite 763 6272 Lang Bypass, New Xochitlport, VT 72704-3308

Phone: +22014484519944

Job: Banking Officer

Hobby: Sailing, Gaming, Basketball, Calligraphy, Mycology, Astronomy, Juggling

Introduction: My name is Rev. Leonie Wyman, I am a colorful, tasty, splendid, fair, witty, gorgeous, splendid person who loves writing and wants to share my knowledge and understanding with you.