Download presentation
Presentation is loading. Please wait.
Published byTimothy Gray Modified over 8 years ago
1
From C to C++
2
What is C++ C++ is the work of Bjarne Stroustrup of AT&T Bell Labs. C++ is a mostly upward compatible extension of C that provides: A better C Support for Data Abstraction Support for Object-Oriented Programming C++ supports these paradigms; it does not require their use
3
Declaring Variables & I/O in C++ #include // include using namespace std; void main() { int a, b; // Declare two integer variables cin>>a;// Get initial value from keyboard cin>>b; cout <<a<<b; // printf(“%d%d”,a,b); cout <<a<<“ ”<<b; // printf(“%d %d”,a,b); int c; // declare a new variable c=a+b; cout<<c<<endl;// printf(“%d\n”,c); int d = a*c; // declare a new variable cout<< d; cin>>d>>c;// scanf(“%d%d”,&d,&c); cout<<d<<c;}
4
Strings in C++ string” in C++ ~ null terminated char arrays in C you have to include header string #include using namespace std; main() { string s1= "HELLO";string s2=s1; string s3 (10,'x'); // a string consisting of 10 x’s cout<<s1 <<endl <<s2 <<endl <<s3 << endl; cout<< s1.length()<<endl; // will return a 5 cin>> s1; cout<<s1 <<endl; cout<<s1+s2; s3=s1+s2; if(s3>s1) cout<<“greater”; }
5
Call by value, call by reference Call by reference in C use pointers Call by reference in C++ use references Reference == name alias int x; int * p=&x; *p=6; int & ref =x; x=3; ref=3; x … ref … * p 3
6
Call by Reference : SWAP example #include using namespace std; void swap (int &, int &); void main() {int i=7 ; int j=-3; swap (i,j); cout <<i<<endl; cout <<j<<endl;} void swap (int & a, int & b) {int t= a; a=b; b=t;}
7
C++ Functions C++ allows to specify default values for function parameters…. void f (int v, float s=3.14, char t=‘\n’, string msg =“Error!!”); Valid invocations: f(12, 3.5, ‘\t’, “OK”); f(12, 3.5, ‘\t’); f(12, 3.5); f(12);
8
Overloading functions #include using namespace std; // C++ checks the number and type of parameters // to decide which overloaded function to use void func( int a) { cout <<a <<endl; } void func (double a) { cout << a +1<<endl; } int main() { int x=8; double y=8; func(x); func(y); return 0;}
9
Some Features of C++ ● Encapsulation (functions methods) Information Hiding (public & private) ● Inheritance (code reuse) vehicle car truck bus ● Polymorphism (having many forms) two_dim triangle ( area of triangle??) square ( area of square??) ADT (stacks)
10
Encapsulation Encapsulation is the process of combining data and functions into a single unit called class C ---------- C++ struct ---------- class Using the method of encapsulation, the programmer cannot directly access the data, data is only accessible through the functions present inside the class Data encapsulation led to the important concept of data hiding
11
Information Hiding In C all members of a structure is accessible. struct Point { int x; int y; int len;}; // members are always public main() {Point obj; obj.x=7; obj.y=9;} In C++ members of a class may be public, private or protected class Point{private : int x; int y; public : int len; }; main() {Point obj; obj.len=8; // x and y cannot be accessed !!!! obj.x=7; obj.y=9; }
12
Encapsulation & Information Hiding struct Point { int x; int y; }; void printpoint(Point obj) {printf(“%d %d”, obj.x, obj.y);} main() {Point obj; printpoint(obj); } class Point { private : int x; int y; public : void printpoint() { cout<<x<<“ “<<y;} }; main() {Point obj; obj.printpoint(); }
13
Inheritance Creating or deriving a new class using another class as a base is called inheritance in C++ The new class created is called a Derived class and the old class used as a base is called a Base class
14
Inheritance Examples
15
Inheritance Example class Circle {// BASE CLASS public: double setRadius(double x) { radius=x;} double Circumference() { return 2*3*radius;}; double Area() { return 3*radius*radius;}; private: double radius; }; class Cylinder : public Circle{ // DERIVED CLASS public: double Volume() { /* ????????????????????*/ }; void setHeigth(double h); private: double height;}; main() {Circle obj1; Cylinder obj2; double v; double a; obj1.setRadius(3); a= obj1.Area (); obj2.setRadius(3); obj2.setHeigth(6); v= obj2.Volume(); // a?? v?? }
16
Polymorphism Polymorphism is the ability to use an operator or function in different ways Types of Polymorphism: Virtual functions Function name overloading Operator overloading
17
Polymorphism Example: Area methods for Circle and Cylinder classes class Circle {// BASE CLASS public: double setRadius(double x) { radius=x;} double Circumference() { return 2*3*radius;}; virtual double Area() { return 3*radius*radius;}; double getRadius() { return radius;} private: double radius; }; class Cylinder : public Circle{ // DERIVED CLASS public: double Volume() { return 3* getRadius() *getRadius()*height; }; void setHeigth(double h); double Area () {2*3*getRadius()*height;} private: double height;}; main() {Cylinder obj1; Cylinder obj2; double a1, a2; obj1.setRadius(3); a1= obj1.Area (); obj2.setRadius(3); obj2.setHeigth(6); a2= obj2.Area(); // a1?? a2?? }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.