Learn C++ as its own language
C++ supports procedural programming, value-oriented design, classes, generic programming and resource management. It shares syntax with C, but a good C++ program should use its own standard library and ownership tools rather than merely translating C allocation patterns.
First complete program
#include
#include
int main() {
std::string student = "Asha";
int completed = 3;
std::cout << student << " completed " << completed << " lessons.\n";
return 0;
} Expected output: Asha completed 3 lessons. std::string owns a sequence of characters. std::cout is the standard output stream. The insertion operator sends successive values to the stream. The std:: prefix identifies names from the standard namespace.
Build model
A compiler translates each source file, and the linker connects the resulting object files and required libraries. With an installed GCC toolchain, a typical build is g++ -std=c++17 -Wall -Wextra -pedantic hello.cpp -o hello. Use the C++ driver, not an assumed C compiler command. Run the executable using your shell or the online editor's Run control.
These lessons use widely available C++17 features unless stated otherwise. The selected compiler standard matters: a feature from a later standard is not guaranteed to work in an older mode. Compiler warnings are useful evidence, but a warning-free build does not establish correct logic.
Core design ideas for this course
- Types: express what an object can represent and which operations are valid.
- Values: strings and containers can manage their own storage.
- Classes: combine state with operations that protect its rules.
- RAII: bind a resource's lifetime to an object's lifetime so cleanup follows scope exit.
- Algorithms: use standard operations over iterator ranges instead of rewriting every loop.
Avoid common starting mistakes
Do not add a global using namespace std; to a header. It can change name lookup for every file that includes it. Do not use new/delete just to store a simple list when std::vector already handles ownership. Do not describe every pointer as owning memory: an ordinary pointer can simply observe an existing object.
Practice and answers
- Change completed to 5; the message should report five lessons.
- Add a second string for the course name and print it on a new line.
- What does
std::mean? It qualifies a name in the standard namespace. - Why use a .cpp extension and C++ compiler mode? The file contains C++ library facilities and syntax that must be translated as C++.
हिन्दी संकेत: C++ सीखते समय std::string, std::vector और automatic lifetime को शुरुआत से अपनाएं। अगला अध्याय program structure और input streams पर है।