![]() |
Basics of C++ - Printable Version +- Sinisterly (https://sinister.ly) +-- Forum: Coding (https://sinister.ly/Forum-Coding) +--- Forum: Coding (https://sinister.ly/Forum-Coding--71) +--- Thread: Basics of C++ (/Thread-Basics-of-C) |
Basics of C++ - vluzzy - 01-02-2024 ++ is a powerful and versatile programming language that supports both procedural and object-oriented programming paradigms. Here are some fundamental concepts in C++: Variables and Data Types: cpp int age = 25; double salary = 50000.50; char grade = 'A'; Control Structures: cpp if (age < 18) { cout << "You are a minor." << endl; } else if (age >= 18 && age < 60) { cout << "You are an adult." << endl; } else { cout << "You are a senior citizen." << endl; } Loops: cpp for (int i = 0; i < 5; ++i) { cout << i << endl; } while (age < 30) { cout << "Still young!" << endl; ++age; } Functions: cpp void greet(string name) { cout << "Hello, " << name << "!" << endl; } greet("Alice"); Arrays and Vectors: cpp int numbers[5] = {1, 2, 3, 4, 5}; vector<int> nums = {1, 2, 3, 4, 5}; Classes and Objects: cpp class Car { public: string brand; string model; int year; }; Car myCar; myCar.brand = "Toyota"; myCar.model = "Camry"; myCar.year = 2020; File Handling: cpp #include <fstream> ofstream outfile("example.txt"); outfile << "Hello, C++!" << endl; outfile.close(); Example Script: Basic Input and Output cpp #include <iostream> using namespace std; int main() { cout << "Enter your name: "; string name; cin >> name; cout << "Enter your age: "; int age; cin >> age; cout << "Hello, " << name << "! You are " << age << " years old." << endl; return 0; } Explanation: The script prompts the user to enter their name and age. It uses cin to read input and cout to display output. Basic input/output is a fundamental part of C++ programming |