1 | C Language > | Developed by Dennis Ritchie in AT & T’s Bell Laboratories of USA in 1972. AT & T > American Telephone & Telegraph Company | Features > Simple, Portable, Mid Level Programming Lang., Structured Prog. Lang., have Rich Library, do Memory Management, Fast Speed Language, Use Pointers & Arrays, Use Recursion, Its Extensible | Portable (Program can run on any m/c which support C), Mid Level Programming Lang. (feature of both low level & high level lang.), Structured Prog. Lang. (can be divide in small logical functional module), Arrays (One Varialble can store multiple values of same data types in Consecutive Memory Address), Pointers (is a variable that store the value of other variable), Recursion (the form of function that calls itself where one of the instructions repeat the process), Extensible (can expand from its initial state) | Complier built in C, Drivers built in C, OS parts built in C, Embedded Chip programs built in C & Many more. | Program Execution : Source Code (.c File) > Complier build object File by linking libraries (.obj File) > Then during run time (.exe file) built in the same folder where source code file placed | ||
2 | Types of Languages > | Low Level, Mid Level & High Level > | Low Level: Machine understandable, Middle Level: features of both languages like C programming which direct access system registry and memory and also its source code is human understandable, High Level: Human understandable language (uses GUI & m/c code independent) | C Parts used in learning of other Language > | Classes, Objects, Inheritance, Polymorphism, Templates, Exception Handling, References etc | Polymorphism > means many forms, when there is hierarchy of classes and related by inheritance. Call to member fx. Will cause diff. fx to execute | ||
3 | Program Layout > | Header Files > Header > Content > Footer | ||||||
4 | Object | |||||||
5 | Object Oriented Programing Structure > | is a programming concept that works on the principle that objects are the most important part of your program. It allows users to create the objects that they want and then create methods to handle those objects. | ||||||
6 | Variable | |||||||
7 | Consists | Alphabets, Digits, Special Symbols | Constants, Variables, Keywords | Instructions / Statements | Program | |||
8 | Constants > | Primary Constant > | Integer, Real, Character > | Real Constant are called floating points, is in fractional (-+ 32.32) & in Exponential Form (32e-58, Left of e is Mantisa & Right is Exponent) | Character Constant form > ‘a’ or ‘5’ or ‘=’ | Integer range for 16 Bit compiler = -+32768, more for 32 bit compiler | ||
Secondary Constant > | Array, Pointer, Structure, Union, Enum | |||||||
9 | Variable Name > | In Length of 1 to 31 Characters with combination of Alphabets, Digits & _ | ||||||
10 | Keywords > | Are 32 in number | auto, double, break, else, case, enum, char, extern, const, float, continue, for, default, goto, do, if, int, long, register, return, struct, short, signed, sizeof, static, struct, switch, typedef, union, unsigned, void, volatile, while | |||||
11 | Comments > | // | Single Line Comment | |||||
/*……*/ | Anywhere comment | |||||||
12 | Function > | main () > is the collective name given to set of statements, all statements belong to this main fx. Statements Are enclosed in braces { } | { Statement 1; | Use with statement terminator > ; | ||||
Statement 2; } | Use with statement terminator > ; | |||||||
13 | Display on screen > | printf (“format string”, Variable) ; | %d – for Integer Output | %s – for string Output | \n – New line use in printf statement to execute the next step in new line | |||
%f – for float / Real | %u – for unsigned Integer Output | |||||||
%c – for character ‘c’ | %ld – for long Integer Output | |||||||
14 | Taken input from keyboard and store it in variable | scanf (“format string”, &Variable) ; | ||||||
15 | Types of Instructions > | Declaration Instruction > | Use = | Assignment operator | ||||
Arithmetic Instruction > | Use Operators, Hierarchy is !, * / %, + -, < > <= >=, == !=, && ||, = | Exponentiation > a=Pow (3,2); | > 3 raise to power 2 | .++a; Pre Increment Operator > show result by already adding, a++; Post Increment > show result for next step by adding after showing first result | ||||
Control Instruction > | Sequence Control > | Execute Statements in Sequence > | Inner bracket statement runs 1st > { (4(3(2(1) ) ) ) } | |||||
Selection / Decision Control > | if, else | |||||||
Repitition / Loop Control > | for, do, while | |||||||
Case Control > | Switch | |||||||
16 | Decision Control Structure > | if > Value comes non zero then it is true | if (this condition is true) execute this statement ; | if (expression) | ||||
{ Statement 1 ; | ||||||||
Statement 2 ; } | ||||||||
16.2 | if – else > | if (expression) | ||||||
{ Statement 1 ; | ||||||||
Statement 2 ; } | ||||||||
else | ||||||||
{ Statement 1 ; | ||||||||
Statement 2 ; } | ||||||||
16.3 | Nested if – elses > | if (expression) | ||||||
{ Statement 1 ; | ||||||||
Statement 2 ; } | ||||||||
else | More than 1 condition by && i.e. if ((condition) && (condition)) | |||||||
{ | { Statement 1; | |||||||
if (expression) | Statement 2;} | |||||||
{ Statement 1 ; | More than 1 condition by && i.e. if ((condition) || (condition)) | |||||||
Statement 2 ; } | { Statement 1; | |||||||
else | Statement 2;} | |||||||
{ Statement 1 ; | ||||||||
Statement 2 ; } | ||||||||
} | ||||||||
16.4 | Another way else – if > | if (expression) Statement; | Nested :- if ( ) { ;} else { if () { ;} else { ;}} | |||||
else if (expression) Statement; | if ((Condition 1) && (Condition2)) Statement : | |||||||
else if (expression) Statement; | ||||||||
else Statement; | ||||||||
16.5 | Conditional Operator > if — then — else, are sometimes called ternary operators since they take 3 arguments | (expression 1 ? Expression 2 : expression 3); | int x, y; | |||||
y= ( x>5 ? 3 : 4 ) | This statement will store 3 in y if x > 5 else store 4 in y | |||||||
17 | Loop Control Instruction > | While > | count = 1; | |||||
While (count <=3) | ||||||||
{ Statement 1; | ||||||||
Statement 2; | ||||||||
count = count +1; } | ||||||||
17.2 | for > | for (initialise counter ; test counter ; increment counter) | for (count=1 ; count <= 3 ; count = count+1) | |||||
{ do this : | { Statement 1; | |||||||
and this ; | Statement 2; | |||||||
and this ; } | Statement 3; } | |||||||
17.3 | Nesting of Loops > | int r, c, sum ; for ( r=1; r<=3; r++) { for (c=1; c<=2; c++) { sum = r + c ; printf ( “r = %d c = %d sum = %d\n”, r, c, sum ) ; } } | ||||||
17.4 | Odd / Infinite loop > do – while > | char another ; int num ; do { printf ( “Enter a number ” ) ; scanf ( “%d”, &num ) ; printf ( “square of %d is %d”, num, num * num ) ; printf ( “\nWant to enter another number y/n ” ) ; scanf ( “Space%c”, &another ) ; } while ( another == ‘y’ ) ; | < Infinite loop with consent, Can also use for in case of while, space if must before %c to run this program – don’t know why | #include <stdio.h> main() { int a=0; do { printf(“Value of a is = %d\n”,a); a++; } while (a<=4); } | ||||
18 | Break Statement | main( ) { int i=1, j=1; while ( i++ <= 100 ) { while ( j++ <= 200 ) { if ( j == 150 ) break ; else printf ( “%d %d\n”, i, j ) ; } printf(“i=%d & j=%d\n”, i,j); } | ||||||
19 | Continue Statement | main( ) { int i,j; for ( i = 1 ; i <= 2 ; i++ ) { for ( j = 1 ; j <= 2 ; j++ ) { if ( i == j ) continue ; printf ( “\n%d %d\n”, i, j ) ; } } } | ||||||
20 | Case Control Instruction > | Switch > The control statement that allows us to make decision from number of choices | ||||||
switch ( integer expression ) { case constant 1 : do this ; case constant 2 : do this ; case constant 3 : do this ; default : do this ; } | main() { int i = 2 ; switch ( i ) { case 1 : printf ( “I am in case 1 \n” ) ; break; case 2 : printf ( “I am in case 2 \n” ) ; break; default : printf ( “I am in default \n” ) ; } } | #include <stdio.h> main() { char optr; int a, b, res; printf(“\nEnter the Operator = \n”); scanf(“%c”, &optr); printf(“Enter the value for a = “); scanf(“%d”, &a); printf(“\nenter the value for b = “); scanf(“%d”, &b); switch (optr) { case ‘+’: res=a+b; printf(“\nSum is = %d”,res); break; case ‘-‘: res=a-b; printf(“\nMinus is = %d”,res); break; case ‘/’: res=a/b; printf(“\nDiv. is = %d”,res); break; | ||||||
default : printf(“\nOnly input operator b/w + – / \n”); } } | ||||||||
21 | Called & Calling Function > | A function is self contained block of statements that perform a coherent task of some kind > | Calling Function > By default, data type is int | main ( ) | #include <stdio.h> main() { int a,b, sum; printf(“enter the value for a = “); scanf(“%d”, &a); printf(“\n enter the value for b = “); scanf(“%d”, &b); | |||
{ message (); printf(“I came back to main now”); } | sum=calsum(a,b); printf(“\nSum of both the numbers are = %d\n”, sum); } | calsum a, b here pass on to calsum x, y | ||||||
Called Function > Its Execute 1st | message ( ) | calsum(x,y) int x,y; { int z; z=x+y; return(z); } | Scope of variable is local to that fx only i.e. z to calsum only i.e. why it is need to return the z to calling fx | |||||
{ printf(“I am in message now”);} | ||||||||
21.1 | If Called Function does not to return any Value then | Void Display ( ) | ||||||
{ | ||||||||
Printf ( ); | ||||||||
Printf ( ); | ||||||||
} | ||||||||
21.2 | Clear Screen Function | Clrscr () ; | #include <conio.h> clrscr ( ) ; main ( ) { } | |||||
22 | Preprocessor Directive > | It is program that processes our program before it passes to compiler | MACROS (#define) | #define UPPER 25 main( ) { int i ; for ( i = 1 ; i <= UPPER ; i++ ) printf ( “\n%d”, i ) ; } | During preprocessing, the preprocessor replaces every occurrence of UPPER in the program with 25 | #define AREA(x) ( 3.14 * x * x ) main( ) { float r1 = 6.25, r2 = 2.5, a ; a = AREA ( r1 ) ; printf ( “\nArea of circle = %f”, a ) ; a = AREA ( r2 ) ; printf ( “\nArea of circle = %f”, a ) ; } | main( ) { #ifdef OKAY statement 1 ; statement 2 ; /* detects virus */ statement 3 ; statement 4 ; /* specific to stone virus */ #endif statement 5 ; statement 6 ; statement 7 ; } | Here, statements 1, 2, 3 and 4 would get compiled only if the macro OKAY has been defined, and we have purposefully omitted the definition of the macro OKAY. At a later date, if we want that these statements should also get compiled all that we are required to do is to delete the #ifdef and #endif statements. |
Header file > (#include) | # include <stdio.h > | Standard Input / Outpu fx | ||||||
# include <math.h > | Mathematical / Airthmetical fx | |||||||
# include <conio.h > | Console Input / Output fx | |||||||
Conditional Compilation | (#ifdef – #else – #endif, #if and #elif, #undef and #pragma) | See above — | ||||||
23 | Pointers > (Address of Memory) | main( ) { int i=3; printf ( “\nAddress of i = %u”, &i ) ; printf ( “\nValue of i = %d”, i ) ; } | Address of i = 65524, Value of i = 3 | printf ( “\nValue of i = %d”, *( &i ) ) ; | &i give address of memory *(&i) gives value stored at that memory address | |||
24 | Storage Classes | Memory & CPU Registers | ||||||
25 | Arrays > | Set of similar data types called arrays and stored in consecutive memory locations | In other words, one variable stores many value at a time is called array | array mentioned in [ ] brackets | Let us try to write a program to find average marks obtained by a class of 30 students in a test. main( ) { int avg, sum=0; int i ; int marks[30] ; /* array declaration */ for ( i = 0 ; i <= 29 ; i++ ) { printf ( “\nEnter marks ” ) ; scanf ( “%d”, &marks[i] ) ; /* store data in array */ } for ( i = 0 ; i <= 29 ; i++ ) sum = sum + marks[i] ; /* read data from an array & add all in sum*/ avg = sum / 30 ; printf ( “\nAverage marks = %d”, avg ) ; } | 2D Array :- main( ) { int stud[4][2] ; int i,j; for ( i = 1 ; i <= 4 ; i++ ) { printf ( “\n Enter Marks1. and Marks2” ) ; scanf ( “%d %d”, &stud[i][0], &stud[i][1] ) ; } for ( i = 1 ; i <= 4 ; i++ ) printf ( “\n%d %d”, i, stud[i][0], stud[i][1] ) ; } | ||
26 | Enum (Enumeration) | User defined Data Type > | enum month {JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,DEC}; enum month rmonth; //First Line Creates “User Defined Data Type” called month. //In the second line “rmonth” variable is declared of type “month” rmonth = FEB; //Default Numeric value assigned to first enum value is “0” Numerically JAN is given value “0”. FEB is given value “1”. MAR is given value “2”. APR is given value “3”. MAY is given value “4”. JUN is given value “5”. and so on….. //printf(“%d”,rmonth); It will Print “1” on the screen because “Numerical Equivalent” of “FEB” is 1 and “rmonth” is initialized by “FEB” | #include< stdio.h> void main() { int i; enum month {JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,DEC}; clrscr(); for(i=JAN;i<=DEC;i++) printf(“\n%d”,i); } Output : 0 1 2 3 4 5 6 7 8 9 10 11 | ||||
27 | .+= & -= Operator > | b+=a | Show result by already adding a in b | Similarly > *=, /=, %= | ||||
b-=a | Show result by already substract a in b and show the results in contineous form by preserving earlier value for variable | |||||||
28 | Print of Size of Char, float, double, int > | printf (” Size of Char %d”, Size of (Char)); | printf (” Size of float %d”, Size of (float)); | printf (” Size of double %d”, Size of (double)); | ||||
29 | Decimal position limit in float > | %.2f | ||||||
30 | ASCII Value | A Character variable holds an integer number from 0 to 127 rather then character itself called ASCII Value | #include <stdio.h> main() { int x = ‘A’; printf (” ASCII Value of A as Character = %c & its Integer value is = %d \n”, x,x); } | Answer is 65, x is the variable taken to print A Value | ||||
31 | Recursion | A function is called ‘recursive’ if a statement within the body of a function calls the same function. | Means the set of statements with in the fx called itself | main( ) { int a, fact ; printf ( “\nEnter any number ” ) ; scanf ( “%d”, &a ) ; fact = factorial ( a ) ; printf ( “Factorial value = %d”, fact ) ; } factorial ( int x ) { int f=1,i; for ( i = x ; i >= 1 ; i– ) f=f*i; return ( f ) ; } | And here is the output… Enter any number 3 Factorial value = 6 | main( ) { int a, fact ; printf ( “\nEnter any number ” ) ; scanf ( “%d”, &a ) ; fact = rec ( a ) ; printf ( “Factorial value = %d”, fact ) ; } rec ( int x ) { int f; if ( x == 1 ) return ( 1 ) ; else f = x * rec ( x – 1 ) ; return ( f ) ; } | ||
32 | Union | |||||||
C++ | ||||||||
1 | Function | Also Called Method, Sub-routine, Procedure etc | return_data_type function_name(Parameters_data_type Parameters_name) { body of the function } | In case there is nothing to return by function then use keyword void at place of return data type | If no any parameters to pass then keep the parameters braces blank | Call by Value, Address, Reference | sum (a, b) => calsum (x, y), a & b are called arguments and x & y are called parameters | |
2 | Array | One variable can store multiple Similar data types values in consecutive memory locations, It is data structure | data_type array_name[ size of array in terms of nos] | int array[5] or int array[5] = {1, 2, 3, 4, 5} | Index start from 0 | Array can be 1D or 2D like int ar[2][5] / ar[rows][columns] | by using :- #include <string> | intialize from 0th row & 0th column |
3 | String | char array_name[ size of array in terms of nos] | char greeting[] = “Hello” or char greeting[6] = {‘H’, ‘E’, ‘L’, ‘L’, ‘O’, ‘\0’} | also have sub strings too | ||||
4 | Print at Screen | cout << “Greeting Message :”; | cout << greeting << endl; | Result :- Greeting Message : Hello | OR cout << “Greeting Message :” << greeting << endl; | |||
5 | Creator | Bjarne Stroustrup in 1980 at Bell Labs | It is extension of C Languages i.e. C languages with classes | |||||
6 | First C++ Program | #include <iostream> using namespace std; int main() { cout <<” C++ is the Extension of C Language\n\n”; } | ||||||
7 | Program Structure | Header Files :- | Global Declaration :- | Class Declaration :- | Main Function :- | Method Definition :- | ||
#include <iostream> using namespace std; | int main() | { cout <<” C++ is the extention of C Language\n\n”; } | ||||||
OR #include<iostream.h> | ||||||||
8 | User Input Storing | cin >> variable_name; | #include <iostream> using namespace std; int main() { int num1, num2, sum; cout <<“Enter the value for num1 = “; cin >> num1; cout <<“Enter the value for num2 = “; cin >> num2; sum=num1+num2; cout <<“Sum of both the numbers are = ” <<sum <<endl; | OR cout <<“Enter the value for num1 2nd time = “; cin >> num1; cout <<“Enter the value for num2 2nd time = “; cin >> num2; cout <<“Addition of both numbers = ” <<num1+num2; cout<<“\n\n”; } | ||||
9 | New Line | cout <<……”\n or ,n”; | cout << “My name is nKamal”; | cout <<” My name is Kamal” <<endl; | ||||
10 | Library Function | setw() | set the number of characters to be used as field width for inserting | #include <iostream> #include <iomanip> using namespace std; int main() { cout <<setw (12); cout <<“Kamal9999”; } | Result :- _ _ _Kamal9999 | #include <iostream> #include <iomanip> using namespace std; int main() { int maxcount=4; int width=6; int row; int column; for(row=1; row<=10; row++) { for(column=1; column<=maxcount; column++) { cout <<setw (width) << row * column; } cout <<endl; } } | by using :- #include <iomanip> | |
11 | Keywords | are 48 in number | asm, auto, break, case, catch, char, class, const, continue, default, delete, double, else, enum, extern, float, for, friend, goto, if, inline, int, long, new, operator, private, protected, public, register, return, short, signed, sizeof, static, struct, switch, template, this, throw, try, typedef, union, unsigned, virtual, void, volatile, while | |||||
12 | Diff. b/w while & do while loop | While | Do – while | |||||
It is exit controlled loop | It is entry controlled loop | |||||||
It execute the statements once and then check loop condition | It 1st check the loop condition, if it is true then executes the statements | |||||||
13 | Continue | Skip the statement next to continue and exit the control to while | for loop > after continue, increament / decreament statement get executed | |||||
while loop > after continue, control goes to condition statement | ||||||||
do – while loop > after continue, control goes to condition statement specified in while | ||||||||
14 | Break | after break, Terminate the loop or control give outside of condition | ||||||
15 | Inheritance | Base class – Main class | Derived Class – class doing inheritance and acquires the properties of main class | Types :- Single, Multiple, Multi-Level & Hierarchical, Hybrid | ||||
16 | Overloading | All values of one object can be copy to other object | Return_type operator=(const class_name &) | #include <iostream> using namespace std; class Marks { private: int m1; int m2; public: Marks(int i, int j) { m1=i; m2=j; } void operator=(const Marks &M) { m1=M.m1; m2=M.m2; } | ||||
void Display() { cout <<“Marks of 1st Subject : ” <<m1 <<endl; cout <<“Marks of 2nd Subject : ” <<m2 <<endl; } }; int main() { Marks Mark1(45, 89); Marks Mark2(36, 59); cout <<“Marks of 1st Student :-” <<endl; Mark1.Display(); cout <<“Marks of 2nd Student :-” <<endl; Mark2.Display(); Mark1=Mark2; cout <<“Marks of 1st Student :-” <<endl; Mark1.Display(); return 0; } | ||||||||
17 | Constructor | 3 types | Default, Parameterized, Copy Constructor > Respectively | #include <iostream> using namespace std; class Marks { public: int m1; int m2; Marks() { m1=9; m2=9; } void Display() { cout <<” Math : ” <<m1 <<endl; cout <<” Science : ” <<m2 <<endl; } }; int main() { Marks m; m.Display(); return 0; } | #include <iostream> using namespace std; class Marks { public: int m1; int m2; Marks(int i, int j) { m1=i; m2=j; } void display() { cout <<” Math : ” <<m1 <<endl; cout <<” Science : ” <<m2 <<endl; } }; int main() { Marks m(40, 80); m.display(); } | #include <iostream> using namespace std; class Marks { public: int m1, m2; Marks() { m1= 60; m2=70; } Marks (const Marks &obj) { m1=obj.m1; m2=obj.m2; } void display() { cout <<” Math : ” <<m1 <<endl; cout <<” Science : ” <<m2 <<endl; } }; int main() { Marks mx; Marks my(const Marks &mx); my.display(); return 0; | ||
18 | Bitwise Operator | |||||||
19 | Destructor | Free Memory | #include<iostream> using namespace std; class Marks { public: int maths; int science; //constructor Marks() { cout << “Inside Constructor”<<endl; cout << “C++ Object created”<<endl; } //Destructor ~Marks() { cout << “Inside Destructor”<<endl; cout << “C++ Object destructed”<<endl; } }; int main( ) { Marks m1; cout<<“Hello World !!” <<endl; cout<<“Hello World !!” <<endl; cout<<“Hello World !!” <<endl; cout<<“Hello World !!” <<endl; return 0; } | |||||
20 | Class | Scope | Member | Object | Object.Member | Abstract Class | ||
public (class member can access anywhere in program)/ protected (class member can access in inheritance class & in parent class)/ private (class member can only access in parent class) | #include <iostream> using namespace std; class shape { protected: int width; int height; public: void setwidth(int w) { width = w; } void setheight(int h) { height = h; } }; class paintcost { public: int getcost(int area) | |||||||
{ return area * 70; } }; class rectangle: public shape, public paintcost { public: int getarea() { return (width * height); } }; int main() { rectangle rect; int aria; rect.setwidth(5); rect.setheight(6); aria = rect.getarea(); cout <<“Total Area : ” <<rect.getarea() <<endl; cout <<“Total Paint Cost : $” <<rect.getcost(aria) <<endl; return 0; | ||||||||
21 | Polymorphism | |||||||
22 | Data Abstraction | execute result w/o knowing to outer world by a class by hiding implementation details | Data Encapulisation :- is the mechanism to building the data and functions which use them | |||||
23 | Library – fstream | read & write from file > | ofstream / ifstream / fstream > | creat file & write information to files / read information from file / does both tasks | also have sub-strings | |||
23 | Dynamic Memory | |||||||
24 | Templates | |||||||
25 | Namespaces | |||||||
26 | Pointer to Array | #include <iostream> using namespace std; int main() { double ar[5]={10, 20, 30, 40, 50}; double *ptr; ptr=ar; cout <<“Value of Array Elements are :” <<endl; for (int i=0; i<5; i++) { cout <<” *(ptr + ” <<i <<“) : “; cout << *(ptr+i) <<endl; } } |