DEF & FACTS! C + 1 = C++ C & C++ ARE MIDDLE LEVEL LANGUAGES INTRODUCTION BRIEF REVIEW OF C NEED OF C++ © arbit club cu 2010
C++ C B A C was designed by Ritchi, Tompson, Kernighan It was fast, good & powerful but program’s maintance was a nightmare! Solution:... Object-Oriented Programming © arbit club cu 2010
DEF & FACTS! C + 1 = C++ Developed by- Bjarne Stroustrup © arbit club cu 2010 C++
DEF & FACTS! C & C++ ARE MIDDLE LEVEL LANGUAGES A better C A superset of C Created at Bell Labs in the 1980's and called C with Classes Adds additional features to improve the language Adds functions and features to support Object Oriented Programming (OOP) © arbit club cu 2010
OOP OBJECT ORIENTED PROGRAMING? © arbit club cu 2010
OBJECT ORIENTED PROGRAMING Objects (data and functions) are members of user-defined types called classes. A class definition is an extension of a C struct definition. It is made up of declarations of variables and of function prototypes to manipulate those variables. © arbit club cu 2010
OBJECT ORIENTED PROGRAMING The variables are typically declared to be private or protected and the functions are typically declared to be public. structs and classes actually are the same in C++ (both can have member functions or “methods”), but struct members are public by default and class members are private by default. © arbit club cu 2010
Basic concepts of OOP Inheritance Data abstraction Data encapsulation Polymorphism © arbit club cu 2010
Inheritance Inherit the capabilities of class from another class © arbit club cu 2010
Data abstraction Representing essential features without including the background details or explanations. © arbit club cu 2010
Data encapsulation Wrapping up of data & functions into single unit called class © arbit club cu 2010
Polymorphism More than one form © arbit club cu 2010
General things! C++ was originally developed to be the next version of C, not a new language. C + OOP Concepts = c++ © arbit club cu 2010
Similarities Data types Same start #include Same conditions same built-in operators on primitive types (+-/*……) © arbit club cu 2010
Similarities Same built-in control structures if, for, while, switch …. must have a function names “main” to determine where the program starts functions are defined the same way © arbit club cu 2010
Differences C++ has function overloading (two functions may have the same name), in C function names must be unique. C++ has operator overloading (operators can be redefined to do other things) C++ uses “new” and “delete” for dynamic memory management, C uses “malloc” and “free” © arbit club cu 2010
Concepts! Printf-cout Scanf-cin Malloc-new Free-delete No use of %d,%c,%s etc …. © arbit club cu 2010
comparison # include int main(void) { printf(“Hello World\n”); return 0; } Simple c program to print hello world. © arbit club cu 2010
comparison C++ version! © arbit club cu 2010 #include int main(void) { cout << “Hello World\n”; return 0; }
Comparison! \n works both in c and c++ endl can be there in c++ instead of \n endl means end of line Stdio-standard input output Iostream-input output stream Cout-console output Cin-console input <<- output operator >>- input operator © arbit club cu 2010
Iostream iostream header file library #include contains definitions for several classes istreamostream fstreamios and their associated objects cincout cerrclog © arbit club cu 2010
Basics >> Extraction Operator extracts data from input stream. << Insertion Operator inserts data into output stream :: Scope Resolution Operator Cascading- Cout<<“the sum of 2 +5 = “<<2+5<<endl; Cin>>value1>>value2; Same starts from ‘main’ function © arbit club cu 2010
Conio.h for clrscr(); DATA TYPES Fundamentals Char,int,float,double Derived Array,functions,pointers,classes,structu res © arbit club cu 2010
Formatting tip Iomanip.h Setw(6) Sets width to 6 ______r Arithmetic operators +,-,/,*,% Increment/decrement operators ++,-- Relational, =,==,!= Logical ||,&&,! ? operator © arbit club cu 2010
setprecision(5) Gives Class math.h Some useful functions! Ceil Ceil(1.03)- 2 Ceil(-1.03)- -1 Exp Exp(2)e*e Floor Floor(1.03)1 Floor(-1.03)-2 Log log(1.0)gives natural log for 1.0 Pow Power(3.0,0) gives 1 Power(4,2)gives 16 © arbit club cu 2010
Math.h Sqrt Sqrt(81.0)gives 9 These are some useful math.h functions you can scan books for more functions! © arbit club cu 2010
Selection statements! If(condition) Statement; Else(condition) Statement; Else Statement; © arbit club cu 2010
Alternative to if ? Expression ? Expression1 : expression2; E.g. a>b?a=10:b=20; © arbit club cu 2010
switch Switch(ch) { case 1: // break; case 2:// break; case3:// break; default:// } © arbit club cu 2010
Loops!! While Do while For © arbit club cu 2010
Character functions If (isalpha(ch)) Nonzero-alphabet Zero-Otherwise If (isdigit(ch)) Nonzero-digit Zero-Otherwise If (islower(ch)) Nonzero-if lowercase letter Zero-Otherwise If (isupper(ch)) Nonzero-alphabet Zero-Otherwise Ch=‘a’; Cout<<toupper(ch) Gives- A Cout<<tolower(ch) Gives- a © arbit club cu 2010
String functions! Char *str1=“one”; Char *str2=“two”; Strcat(str1,str2) Cout<<str1; //onetwo © arbit club cu 2010
Comparison function Char *str1=“ace”; Char *str2=“bag”; Strcmp(str1,str2) //returns –ve value Strcmp(str2,str1) //returns +ve value © arbit club cu 2010
Copying and length Strcpy(str2,str1) //value of str1 into str2 Strlen(“vikrant”) would give 7 © arbit club cu 2010
Function overloading!! Complete reference pg-275 Constructors-283 Classes basics-290 © arbit club cu 2010
THANKYOU! THANKYOU DOUBTS!! © arbit club cu 2010