Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
C# and Visual Basic are the most popular languages in .NET, but Visual Studio caters to other language enthusiasts too. Today I want to look at the support for C++. I'm inside Visual Studio, and I've opened a solution called CPlus. Inside that solution are two projects: ExploringCPlusClr and ExploringCPlusWin32. There are a variety of different kind of C++ project types. Let's go take a look. File > New, and then I'm going to choose the Project menu. And then I'm going to scroll down to the C++ section.
You can see that there is a CLR Console Application and a Win32 Project. If you are familiar with ATL or MFC, there are those project types in here as well. I've already added the projects to my solution, so I'm going to click Cancel at this point. I'm going to start by looking at the ExploringCPlusWin32 project. This is an unmanaged C++ project. It's not my startup project, however.
I can tell that because it's not in bold. So I'm going to right-click and choose Set as StartUp Project. That way when I run the application, I'll be sure that my code runs. Next, I'm going to open this node and find the Source Files folder and then open the ExploringCPlusWin32.cpp file. The .cpp extension means that this file contains C++ code. I will double-click on the file and then show you the code.
I need a starting point for my application for this console application. That's called _tmain. Within that function, I'm going to write out to the console, and I'm going to read some information in from the console. To do that, I'm going to use the cout and the cin keywords. cout does what it implies; it's console out. And I'm using this insertion character here to take this string and insert it into the output stream.
I'm also going to end the line with the special "endl" keyword. An alternative way of doing this would be to concatenate like this. This is a longer sentence and then by using the \n character within the string, that's also the equivalent of new line, so I don't have to use endl. Line 18 tells the application to wait for user input, to pause until they press the Enter key. Next, I'll declare a variable. That's easy to do.
The data type of the variable is int and then the name of my variable is age. All lines of code in C++ end with semicolons. Then I'm going to output this string, and then on line 22, this is going to take whatever the user types and push it into this variable, using the insertion characters. Next, I'm going to declare a variable of type string called fullName. And there are a couple of ways I can get the information. One is to use this fflush function and pass it these standard input buffer, or if I'd rather, I can use this function down here called getline.
Getline takes two arguments: it takes this input stream from the console, and it takes a variable that I want to store the data in. And then getline calls cin and pushes the data into this fullName variable. Lastly, I'll concatenate them all into a single output. Would you like to see it running? Good! I'll go up to Debug > Start Debugging. Visual Studio realizes that I haven't built this application recently, so it asks me if I would like to build it.
I'm going to say Yes. As you can see, there are my two strings output to the console. It's waiting for me to press a key. Next, it prompts me for my age, and then my full name, and finally it outputs that to the console and then waits for me to kill the application by pressing the Enter key. Next, I would like to show you the managed version of this, the CLR version. It's much easier to program, and it's more like C#.
Because it uses the .NET libraries, it's easy to use the thousands of .NET types that exist in the .NET assemblies. So I'm going to show you that code. I'm going to right-click on this project and make it my startup, Set as StartUp Project. And then I'm going to expand this node, find the Source Files folder, and then double-click on this cpp file right here, the ExploringCPlusClr file. Like the other application, I need a starting function.
Here, it's called main. And then you'll notice that I'm going to be working with .NET classes. The .NET class I want to work with is Console, and the method, or function, that I want to call is called WriteLine. If you're already familiar with C# or Visual Basic, this should look vaguely familiar. The main difference is you use the colon, colon between the class name and the member name. I'm also using a prefix here in the string, the L prefix on the string. So what am I doing in this code? I'm writing a prompt the user on line 11, and then on line 12, I'm declaring a variable named fullName, and the datatype of that is the string.
Then I'm prompting the user, and then I'm reading from the console and pushing the data into this fullName variable. Once I'm done with that, I will write once again to the console, and I'm going to do a concatenation operation, putting the two strings together, and then a blank ReadLine. Would you like to see that running? Okay, here we go. Debug > Start Debugging. Once again, Visual Studio prompts me to build the application. I'll choose Yes. And there's my prompt and your full name.
And there's my concatenation to the output screen. I'll press Enter one more time to kill the application. As you can see, there's still a place for C++ in Visual Studio. Granted, languages like C# are easier to use and generally more popular with the current generation of developers, but if C++ is your language of choice, you should be happy to see it living proudly in Visual Studio.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 105403 Viewers
56 Video lessons · 117106 Viewers
71 Video lessons · 86326 Viewers
131 Video lessons · 41256 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.