DHA Suffa University CS 103 Object Oriented Programming Fall 2015 Lab #01: Introduction to C++ Objective: To Learn Basic input, output, and procedural part of C++. C++ Object-orientated programming language compiled programming language case-sensitive Integrated Development Environment An IDE (Integrated Development Environment) will be used to write programs. IDE is basically a programmer-orientated text editor, which has syntax highlighting and normally compiler integration. Most commonly used IDE for C++ are: Eclipse Dev-C++ Code::Blocks NetBeans IDE Microsoft Visual Studio Express CodeLite Borland C++ Turbo C Data Types C++ Type int Stores Whole numbers (integers) bool True of False (0 or 1) float double char Fractional number (real number) float with twice the precision A single ASCII character Variables: Variables are used to store values. int a; a = 10; int a = 10; declaring variable assigning value to a variable. initialization [assignment with the declaration] Page 1 of 7
Variable name: It cannot be a reserved keyword It doesn t start with a number or a special characer (*; etc) General convention is to start it with a lower case letter C++ Library File <iostream> Standard Input / Output Streams Library Standard input/output stream objects: Objects Operators cin Standard input stream (object) >> extraction operator cout Standard output stream (object) << insertion operator Namespace: A namespace is a declarative region that provides a scope to the identifiers (the names of types, functions, variables, etc) inside it. Namespaces are used to organize code into logical groups and to prevent name collisions that can occur especially when your code base includes multiple libraries. std: The std namespace (where features of the C++ Standard Library, such as string or vector, are declared). Note: you can combine multiple statements into one cout << "Hello, " << "World!"; You can also combine cin statements together in the same way to receive several pieces of input in a row. Enter still needs to be pressed after each one, Lab0101.cpp: // Hello World Program cout << "Hello World"; Page 2 of 7
Lab0102.cpp: // Add two numbers int num1, num2; int sum = 0; cout << "Enter first number : "; cin >> num1; cout << "\n Enter second number : "; cin >> num2; sum = num1 + num2; cout << "\n Sum of two numbers is : " << sum << "\n" ; Operators: Arithmetic operators: Comparison operators: Logical operators: Compound operator: +, -, *, /, %, == equal && AND a+=1 a=a+1 ++, - - (unary operators for increment and decrement)!= not equal < less than OR! NOT a*=b a=a*b > greater than etc. Page 3 of 7
Conditionals: if Statement Syntax: if ( TRUE ) Execute all statements inside the braces If-else Statement Syntax: if ( TRUE ) // Execute these statements if TRUE else // Execute these statements if FALSE If-else if-else Statement Syntax: if ( <condition> ) statement(s) if <condition> is TRUE else if ( <another condition> ) statement(s) if <another condition> is TRUE else Execute when none of the above conditions is TRUE Lab0103.cpp: // Is number positive or negative? int number = 0; cout << "Enter number : "; cin >> number; if (number > 0) cout << "Number is Positive!\n"; else cout << "Number is Negative!\n"; Page 4 of 7
SWITCH CASE Syntax: switch(expression) case value1 : case value2 : case value3 : default : Lab0104.cpp: // C++ program to build simple calculator using switch Statement char op; float num1,num2; cout<<"select an operator either + or - or * or / \n"; cin>>op; cout<<"enter two operands: "; cin>>num1>>num2; switch(op) case '+': cout<<num1<<" + "<<num2<<" = "<<num1+num2; case '-': cout<<num1<<" - "<<num2<<" = "<<num1-num2; case '*': cout<<num1<<" * "<<num2<<" = "<<num1*num2; case '/': cout<<num1<<" / "<<num2<<" = "<<num1/num2; default: cout<<"error! Operator is not correct"; Page 5 of 7
Loop: A loop is a piece of code that is repeated several times, one after another until a certain condition is met. C++ has three types of loop; for loop, while loop and do while loop. for loop Syntax: for ( init; condition; increment ) statement(s); while loop Syntax: while(condition) statement(s); do-while loop Syntax: do statement(s); while( condition ); Lab0105.cpp: // Do-while loop char ch; do cout << "Do you want to play again? (Y/N) " ; cin>>ch; while (ch == 'Y' ch == 'y'); Page 6 of 7
Task#1(lab01T01.cpp) Write a program to accept an integer n from the user and find the sum and average of first n natural numbers. Make sure that user enters positive integer between 1 and 20. Submission Deadline: Sec A : 2nd Sep, 15 Wednesday 11:55 pm Sec B : 4th Sep, 15 Friday 11:55 pm Problem lab01p01.cpp Write a program to print reverse of a number. [If input is 12345, the output will be 54321] Part B Problem lab01p02.cpp Write a program to design a menu with the following options to calculate: 1. Factorial of a number 2. Prime or not 3. Odd or Even 4. Exit [Hint: Use switch statement to select the menu option and perform desired task] Output screen would like this: Page 7 of 7 Copy protected with Online-PDF-NoCopy.com