C++ Introduction
COS2614 - Programming: Contemporary Concepts · Introduction to C++ and Qt 4
C++ Introduction
This topic introduces you to C++, a powerful programming language widely used for software development. Understanding C++ is essential as it lays the foundation for object-oriented programming and is the basis for many modern programming languages. C++ is known for its performance and flexibility, making it suitable for various applications, from system software to game development.
Key idea: After studying this topic, you should be able to:
- Explain the purpose and features of C++.
- Describe the history and evolution of C++.
- Set up a C++ development environment.
- Write and run a simple C++ program.
- Understand basic input and output in C++.
Overview of C++
C++ is an extension of the C programming language, also known as "C with Objects." It enhances C by adding features such as strong typing, data abstraction, and support for object-oriented programming (OOP). C++ allows programmers to work at various levels, from low-level operations to high-level object manipulation.
A Brief History of C++
Bjarne Stroustrup developed C++ at AT&T Bell Labs in the early 1980s. The language evolved over time, with the first edition of "The C++ Programming Language" published in 1986. C++ became standardized in 1998, ensuring its widespread use and recognition.
Setting Up Your Development Environment
To start programming in C++, you need to set up a development environment. This includes installing a C++ compiler and an Integrated Development Environment (IDE). Popular options include:
- GCC (GNU Compiler Collection) for Linux
- MinGW (Minimalist GNU for Windows) for Windows
- Xcode for macOS
Ensure that you have the Qt framework installed for GUI applications. You can download it from the official Qt website.
Your First C++ Program
Here is a simple C++ program that prints "Hello, World!" to the console:
#include <iostream> // Include the iostream library
using namespace std; // Use the standard namespace
int main() {
cout << "Hello, World!" << endl; // Print to console
return 0; // Exit the program
}To compile and run this program:
- Save the code in a file named hello.cpp.
- Open a terminal or command prompt.
- Navigate to the directory where you saved the file.
- Compile the program using:
g++ hello.cpp -o hello. - Run the program using:
./hello(orhello.exeon Windows).
Basic Input and Output
C++ uses streams for input and output. The iostream library provides the cout object for output and the cin object for input. Here is an example that takes user input:
#include <iostream>
using namespace std;
int main() {
int age;
cout << "Enter your age: ";
cin >> age; // Get user input
cout << "You are " << age << " years old." << endl;
return 0;
}Common Mistakes
Watch out: A common mistake is forgetting to include the necessary headers, such as #include <iostream>. This will result in compilation errors.
Summary
- C++ is a powerful programming language, an extension of C.
- It supports various programming paradigms, including object-oriented programming.
- Setting up a development environment is crucial for coding in C++.
- Basic input and output is handled using streams.
Check your understanding
- What are the main features of C++?
- Who developed C++ and when?
- How do you compile and run a C++ program?
- What is the purpose of the
iostreamlibrary?