Chapter 3 Computer Science 12 - Federal Board PDF

Summary

This document provides definitions, concepts, and examples related to programming in C++. It covers data types (integers, floating-point, characters), constants, variables, operators (arithmetic, assignment, relational, logical), functions (cout, cin, getch), escape sequences, manipulators (endl, setw), and example programs.

Full Transcript

# Object Oriented Programming in C++ ## Defining Terms * **Program:** A set of instructions that performs a specific task when executed by a computer. * **Header file:** Contains information required by the program. * **Reserved words:** Special words reserved by the programming language for speci...

# Object Oriented Programming in C++ ## Defining Terms * **Program:** A set of instructions that performs a specific task when executed by a computer. * **Header file:** Contains information required by the program. * **Reserved words:** Special words reserved by the programming language for specific purposes. * **Statement terminator:** Marks the end of a statement (semicolon). * **Comments:** Explanatory statements that help the reader understand the code. * **Constant:** A value that does not change during execution. * **Variable:** A name of memory location that stores data. * **Escape sequence:** Special characters that control output on the output device. * **Operator:** A symbol that tells the computer to perform a specific task. * **Assignment operator:** Assigns a value to a variable (equals sign). * **Arithmetic operator:** Performs arithmetic operations (addition, subtraction, multiplication, division). * **Relational operator:** Compares two values of the same type. * **Logical operator:** Combines two or more conditions (AND, OR, NOT). * **Compound expression:** Combines two or more conditions using logical operators. * **Type casting:** Converts a data type to another. * **Preprocessor directive:** Used to include a header file. * **Manipulator:** Modifies the output in various ways. ## Concepts ### Data types C++ supports the following data types: * **Integer:** Stores whole numbers. * `int`: 4 bytes, -2147483648 to 2147483647 * `unsigned int`: 4 bytes, 0 to 4294967295 * `short int`: 2 bytes, -32768 to 32767 * `unsigned short int`: 2 bytes, 0 to 65535 * `long int`: 4 bytes, -2147483648 to 2147483647 * `unsigned long int`: 4 bytes, 0 to 4294967295 * **Floating Point:** Stores numbers with a fractional part. * `float`: 4 bytes, -3.4E-38 to 3.4E+38 (6 digits of precision) * `double`: 8 bytes, -1.7E+308 to 1.7E+308 (15 digits of precision) * **Character:** Stores a single character. * `char`: 1 byte ### Constants A constant is a value that does not change during execution. ``` const int AGE = 34; ``` ### Variables A variable is a name of a memory location that stores data. ``` int x,y,z; ``` ### Operators Operators perform specific actions based on operands. * **Arithmetic Operators:** * `+`: Addition * `-`: Subtraction * `*`: Multiplication * `/`: Division * `%`: Modulo (remainder) * **Assignment Operators:** * `=`: Assigns a value to a variable * `+=`: Adds and assigns * `-=`: Subtracts and assigns * `*=`: Multiplies and assigns * `/=`: Divides and assigns * `%=`: Modulo and assigns * **Increment/Decrement Operators:** * `++`: Increments by one * `--`: Decrements by one * **Relational Operators:** * `==`: Equal to * `!=`: Not equal to * `<`: Less than * `>`: Greater than * `<=`: Less than or equal to * `>=`: Greater than or equal to * **Logical Operators:** * `&&`: AND * `||`: OR * `!`: NOT * **Ternary Operators:** * `?:`: Conditional Operator ### Functions * **`cout`:** Outputs data on the screen. * **`cin`:** Inputs data from the keyboard. * **`getch()`:** Reads a single character from the keyboard, without pressing Enter. * **`getche()`:** Reads a single character from the keyboard, and displays it on the screen. * **`gets()`:** Inputs a string from the keyboard. * **`puts()`:** Outputs a string to the screen. ### Escape Sequences Escape sequences are special characters that control formatting. * `\a`: Alert (beep) sound * `\b`: Backspace * `\n`: Newline * `\r`: Carriage return * `\t`: Tab * `\\`: Backslash * `\'`: Single quote * `\"`: Double quote ### Manipulators * **`endl`:** Inserts a newline and flushes the output buffer. * **`setw(n)`:** Sets the minimum field width. ## Example Programs ### Program 1: Sum and Product of Three Integers ```c++ #include<iostream.h> #include<conio.h> void main() { int x,y,z,sum,prod; cout<<"\nEnter first number:"; cin>>x; cout<<"\nEnter second number:"; cin>>y; cout<<"\nEnter third number:”; cin>>z; sum=x+y+z; prod=x*y*z; cout<<"\nSum="<<sum; cout<<"\nProduct="<<prod; getch(); } ``` ### Program 2: Area of a triangle ```c++ #include<iostream.h> #include<conio.h> void main() { float base,height,area; cout<< "\nEnter the base:"; cin>>base; cout<< "\nEnter the height:"; cin>>height; area=(base*height)/2; cout<< “\nThe area of triangle is "<<area; getch(); } ``` ### Program 3: Output Formatting with Manipulators ```c++ #include<iostream.h> #include<conio.h> #include<iomanip.h> void main() { int price1=8540,price2=325,price3=27800; cout<< "Product" <<setw(10)<< "Price"<<endl << "Hard disk" <<setw(10)<<price1<<endl << "Mouse" <<setw(10)<<price2<<endl << “Computer" <<setw(10)<<price3<<endl; getch(); } ``` ## Header Files | Header File | Description | |---|---| | `iostream.h` | Basic input/output operations (cin, cout). | | `conio.h` | Console input/output, manages input/output for console based applications. | | `math.h` | Basic mathematical operations (sqrt, pow). | | `string.h` | String manipulation functions (strcmp, strcpy). | | `iomanip.h` | Manipulators (setw, endl, setprecision). | | `time.h` | Date and time operations. | ## Precedence of Operations The operators are evaluated in the following order of precedence: 1. `*`, `/`, `%` 2. `+`, `-` 3. `<`, `<=`, `>`, `>=` 4. `==`, `!=` 5. `!` (not) 6. `&&` (and) 7. `||` (or) 8. `=`, `+=`, `-=`, `*=`, `/=`, `%= ` ## Home Assignments 1. Write a program that reads four integers and prints their sum, product and average. 2. Write a program that reads length and breadth of a rectangle and prints its area. 3. Write a program that reads temperature in Fahrenheit and prints its equivalent temperature in Celsius using the formula: `c = 5/9(f-32)`. This Markdown document provides a structured format for the content of the Object Oriented Programming in C++ document or image. This guide covers key definitions, concepts, and even example programs. Further exercises and home assignments are also provided to expand upon the learned concepts.

Use Quizgecko on...
Browser
Browser