0% found this document useful (0 votes)
6 views3 pages

Lab 1

This document serves as an introduction to C++ programming, covering its fundamentals, syntax, structure, and basic input/output operations. It explains the components of a C++ program, data types, operators, and how to compile and run a program using different methods. Additionally, it includes tasks for practical application of the concepts learned.

Uploaded by

mufassir abbasi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Lab 1

This document serves as an introduction to C++ programming, covering its fundamentals, syntax, structure, and basic input/output operations. It explains the components of a C++ program, data types, operators, and how to compile and run a program using different methods. Additionally, it includes tasks for practical application of the concepts learned.

Uploaded by

mufassir abbasi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Lab 1

Introduction to C++
Objective:

 Understand the fundamentals of C++ programming.


 Learn about syntax, structure, and basic input/output.
 Gain familiarity with C++ compilers.

What is C++?

C++ is a powerful general-purpose programming language that supports procedural, object-


oriented, and generic programming. It is widely used in software development, game
programming, system programming, and real-time applications.

Structure of a C++ Program:

A basic C++ program consists of the following components:

1. Preprocessor Directives: Used to include necessary libraries.

#include <iostream>

2. Namespace: The using namespace std; statement allows usage of standard C++ elements
without prefixing them with std::.
3. Main Function: The entry point of a C++ program.

int main() {
return 0;
}

4. Statements and Expressions: Instructions within the main() function.


5. Comments: Used to make code readable and maintainable.
o Single-line comment: // This is a comment
o Multi-line comment: /* This is a multi-line comment */
Basic Input and Output:

#include <iostream>
using namespace std;

int main() {
cout << "Hello, World!" << endl;
return 0;
}

Output:

Hello, World!

Input using cin

#include <iostream>
using namespace std;

int main() {
int age;
cout << "Enter your age: ";
cin >> age;
cout << "Your age is " << age << endl;
return 0;
}

Example Run:

Enter your age: 25


Your age is 25

Data Types in C++:

C++ provides various data types:

 int (Integer, e.g., 10, -5)


 float (Floating-point, e.g., 10.5, 3.14)
 double (Double-precision floating-point)
 char (Character, e.g., 'A', 'B')
 string (Sequence of characters, e.g., "Hello")
 bool (Boolean, true or false)
Operators in C++:

 Arithmetic Operators: +, -, *, /, %
 Relational Operators: ==, !=, >, <, >=, <=
 Logical Operators: &&, ||, !
 Assignment Operators: =, +=, -=, *=, /=, %=

Compiling and Running a C++ Program:

Using g++ (GNU Compiler) in Terminal:

1. Save the program as program.cpp


2. Open the terminal and navigate to the file location.
3. Compile the program:

g++ program.cpp -o program

4. Run the program:

./program

Using an IDE (e.g., Code::Blocks, Dev-C++, Visual Studio Code):

1. Open the IDE and create a new C++ project.


2. Write the code in the editor.
3. Click "Build and Run" to execute.

Tasks:

1. Write a C++ program to print your name and age.


2. Modify the user input program to accept and display two numbers.
3. Write a program that prints the sum, difference, product, and quotient of two numbers
entered by the user.

You might also like