Department of Computer Science and Engineering: C++ Program Structure
This document provides an overview of key concepts in C++ including program structure, comments, data types, operators, functions, classes, pointers and control statements. It discusses the basic structure of a C++ program with main(), defines different data types and qualifiers, and covers various types of operators. It also describes function prototypes and definitions, class declarations and objects, and accessing class members. Finally, it introduces control statements like if-else, switch case and loops as well as pointer declaration and usage.
Department of Computer Science and Engineering: C++ Program Structure
This document provides an overview of key concepts in C++ including program structure, comments, data types, operators, functions, classes, pointers and control statements. It discusses the basic structure of a C++ program with main(), defines different data types and qualifiers, and covers various types of operators. It also describes function prototypes and definitions, class declarations and objects, and accessing class members. Finally, it introduces control statements like if-else, switch case and loops as well as pointer declaration and usage.
PREPARED BY: Mr. S.Thulasidass A.P-CSE & Ms. J.Jayapriya A.P-CSE
C++ Program Structure: Let us look at a simple code that would print the words Hello World. #include <iostream> // main() is where program execution begins. int main() { cout << "Hello World"; // prints Hello World return 0; }
Comments in C++ C++ supports single line and multi-line comments. All characters available inside any comment are ignored by C++ compiler. C++ comments start with /* and end with */. For example: // This is a single line comment /* C++ comments can also * span multiple lines */ #include <iostream> using namespace std; main() { /* C++ comments This Program prints Hello World */ cout << "Hello World"; // prints Hello World return 0; }
Data Types in C++ Three types: Built in data type Derived data type User-defined data type Data type is one of the fundamental properties of a variable.
1. Built in Data Types: 2. char a single byte that can hold one character. 3. int an integer 4. float a single precision floating point number. 5. double a double precision floating point number. Qualifiers:- short, long, signed, unsigned are called qualifiers. i. Short, long size qualifiers ii. Signed, unsigned sign qualifiers Size qualifiers can not be applied to char, float, data types. Sign qualifiers can not be applied to float, double & long double.
MAILAM ENGINEERING COLLEGE, MAILAM DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING PREPARED BY: Mr. S.Thulasidass A.P-CSE & Ms. J.Jayapriya A.P-CSE
Name Description Size* Range* char Character or small integer. 1 byte signed: -128 to 127 unsigned: 0 to 255 character: a to z A to Z & Special Characters short int Short Integer. 2 bytes signed: -32768 to 32767 unsigned: 0 to 65535 int Integer. 4 bytes signed: -2147483648 to 2147483647 unsigned: 0 to 4294967295 long int Long integer. 4 bytes signed: -2147483648 to 2147483647 unsigned: 0 to 4294967295 bool Boolean value. It can take one of two values: true or false. 1 byte true or false float Floating point number. 4 bytes +/- 3.4e +/- 38 (~7 digits) double Double precision floating point number. 8 bytes +/- 1.7e +/- 308 (~15 digits) long double Long double precision floating point number. 8 bytes +/- 1.7e +/- 308 (~15 digits) 2.Derived Datatypes: Arrays Pointers Function Reference 3.User-Defined Data Types: Struct Union Class Enumeration C++ Operators: An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. C++ is rich in built-in operators and provides following type of operators: Arithmetic Operators ( +, -, \, *, ++, --) Relational Operators (==, !=, >. <, >=, <=) Logical Operators (&&, ||, ! ) Bitwise Operators (& |, ^, ~, <<, >>) Assignment Operators (=, +=, -=, *=, /=, %=, <<=, >>=, &=, ^=, |=) Misc Operators ( sizeof, & cast, comma, conditional etc.) Types of operators: 1. Assignment operators 2. Arithmetic operators 3. Relational operators 4. Logical operators 5. Bitwise operators 6. Compound Assignment Operators 7. Increment & decrement operators 8. Conditional operators 9. Special operator
MAILAM ENGINEERING COLLEGE, MAILAM DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING PREPARED BY: Mr. S.Thulasidass A.P-CSE & Ms. J.Jayapriya A.P-CSE
1.Assignment Operators (=) General syntax: variable=expression; 2.Arithmetic Operators:-
Operator Meaning + Addition or unary plus. - Subtraction or unary minus. * Multiplication. / Division. % Modulo division. 3.Relational Operators:- It is used to make comparisons between two expressions. All these operators are binary and require two operands. Operator Meaning < Less than > Greater than <= Less than equal to >= Greater than equal to = = Equal to != Not equal to 4.Logical Operators: Logical operators are useful in combining one or more conditions. Any expression that evaluates to zero denotes a FALSE condition and that evaluating to non-zero value denotes a TRUE condition. Operator Meaning Type && Logical AND Binary | | Logical OR ! Logical NOT Unary 5.Bitwise Operator: Operator Meaning & Bitwise AND | Bitwise OR ^ Bitwise EX-OR ~ Bitwise complement << Shift left >> Shift right 6.Compound Assignment Operators:
7. Increment and Decrement Operator:- Operator Syntax ++ variable name ++; ++variable name; -- variable name--; -- variable name; Operator Usage Effect + = a + = exp; a = a + (exp); - = a - = exp; a = a - (exp); * = a * = exp; a = a * (exp); / = a / = exp; a = a / (exp); % = a % = exp; a = a % (exp); MAILAM ENGINEERING COLLEGE, MAILAM DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING PREPARED BY: Mr. S.Thulasidass A.P-CSE & Ms. J.Jayapriya A.P-CSE
8.Conditional Operator(TERNARY OPERATOR) ?:
General syntax: exp1? True : False
7.Special Operators:
Sizeof = returns the size of data type or variable in terms of bytes occupied in memory. delete = Memory release operator new = Memory allocation operator * = Indirection operator. & = address operator. , = comma operator. :: = Scope Resolution operator
C++ Identifiers: A C++ identifier is a name used to identify a variable, function, class, module, or any other user-defined item. An identifier starts with a letter A to Z or a to z or an underscore (_) followed by zero or more letters, underscores, and digits (0 to 9). C++ does not allow punctuation characters such as @, $, and % within identifiers. C++ is a case-sensitive programming language. Thus, Manpower and manpower are two different identifiers in C++.
C++ Keywords: The following list shows the reserved words in C++. These reserved words may not be used as constant or variable or any other identifier names. Asm else new this Auto enum operator throw Bool explicit private true Break export protected try Case extern public typedef Catch false register typeid Char float reinterpret_cast typename Class for return union Const friend short unsigned const_cast goto signed using continue If sizeof virtual Default inline Static void Delete int static_cast volatile Do long Struct wchar_t Double mutable Switch while dynamic_cast namespace Template
MAILAM ENGINEERING COLLEGE, MAILAM DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING PREPARED BY: Mr. S.Thulasidass A.P-CSE & Ms. J.Jayapriya A.P-CSE
CONTROL STATEMENT: Conditional Statement Looping Statement Breaking Statement CONDITIONAL STATEMENT: If Statement Ifelse Statement Switch case Statement SYNTAX: if(condition) { //If condition true execute this statement }
switch(variable) { case (value1): { } break; case (value2): { } break; default: break; }
LOOPING STATEMENT for loop while loop do while loop SYNTAX: for (intialise;condition;++/--) { // Statement }
while (condition) { // Statement }
do { // Statement }while(condition);
Break statement: A break construct terminates the execution of loop and the control is transferred to the statement immediately following to the loop. The term break refers to the act of breaking out of a block of code. Fig:break statements in loops; for(initialize;exp1;exp2) { _______;
if(condition) True break; _________; } statement;
MAILAM ENGINEERING COLLEGE, MAILAM DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING PREPARED BY: Mr. S.Thulasidass A.P-CSE & Ms. J.Jayapriya A.P-CSE
Function: Function Prototyping: returntype functionname(argumentlist); Ex: int add(int a, intb); Function Definition: returntype functionname(argumentlist) { // Statements } Ex: int add(int a, int b) { return(a+b); } Function Call: functionname(argumentvalue); Ex: add(2,4);
Call by Value int add(int a,int b); // Call by Value Call by Referrence int add(int &a, int &b); // Call by Referrence int add(int *a, int *b); // Call by Referrence
CLASSES AND OBJECTS Class Declaration class classname { private: //Data member //Member Function public: //Data member //Member Function }; Object Declaration Method 1: Void main() { classname obj1,obj2; } Method 2: class classname { //Data member //Member Function }obj1,obj2;
MAILAM ENGINEERING COLLEGE, MAILAM DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING PREPARED BY: Mr. S.Thulasidass A.P-CSE & Ms. J.Jayapriya A.P-CSE
Accessing Class Members For Data member Objectname.datamember; For Member Function Objectname.memberfunction(argumentlist);
Defining Member Function Inside the Class class classname { returntype functionname(argumentlist) { // Statements } }; Outside the Class class classname { returntype functionname(argumentlist); }; returntype classname::functionname(argumentlist) { // Statements }