From C to C++. 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.

Slides:



Advertisements
Similar presentations
Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
Advertisements

INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
C/c++ 4 Yeting Ge.
EEM 480 Algorithms and Complexity by Assist. Prof. Dr. Emin Germen.
1 CS 201 Introduction to c++ (1) Debzani Deb. 2 History of C++ Extension of C (C++ for better C) Early 1980s: Bjarne Stroustrup (Bell Laboratories) Provides.
// Functions that take no arguments #include using namespace std; void function1(); void function2( void ); int main() { function1(); function2(); return.
Rossella Lau Lecture 1, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 1: Introduction What this course is about:
1 Inheritance Inheritance is a natural way to model the world in object-oriented programming. It is used when you have two types of objects where one is.
Computer Science 1620 Function Scope & Global Variables.
CS1061: C Programming Lecture 22: A Brief Look at C++ A. O’Riordan, 2004.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Introduction to C++ Programming Concept Basic C++ C++ Extension from C.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
C++ fundamentals.
OBJECT ORIENTED PROGRAMMING
1 Review: Two Programming Paradigms Structural (Procedural) Object-Oriented PROGRAM PROGRAM FUNCTION OBJECT Operations Data OBJECT Operations Data OBJECT.
1 C++ Structures Starting to think about objects...
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
Introduction to C++. Overview C++? What are references Object orientation Classes Access specifiers Constructor/destructor Interface-implementation separation.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
Overview of Previous Lesson(s) Over View  OOP  A class is a data type that you define to suit customized application requirements.  A class can be.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
Looking toward C++ Object-Oriented Programming Traditional Programming Procedure-Oriented Programming Task-Based Programming circle method draw data C.draw()
Introduction to C++ Systems Programming. Systems Programming: Introduction to C++ 2 Systems Programming: 2 Introduction to C++  Syntax differences between.
Object Oriented Programming Elhanan Borenstein copyrights © Elhanan Borenstein.
CLASS INHERITANCE WEEK 7 Chapter 20 CS SENEM KUMOVA METIN1.
Rossella Lau Lecture 1, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 1: Introduction What this course is about:
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 24P. 1Winter Quarter C++ Lecture 24.
Copyright  Hannu Laine C++-programming Part 1 Hannu Laine.
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
Prepared by: Elsy Torres Shajida Berry Siobhan Westby.
CS1201: Programming Language 2 Classes and objects By: Nouf Aljaffan Edited by : Nouf Almunyif.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Monday, Mar 31, 2003Kate Gregory with material from Deitel and Deitel Week 12 Labs 4 and 5 are back File IO Looking ahead to the final.
Object-Oriented Programming. Procedural Programming All algorithms in a program are performed with functions and data can be viewed and changed directly.
Chapter 10 Inheritance and Polymorphism
CSC241 Object-Oriented Programming (OOP) Lecture No. 4.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
Lecture 19 CIS 208 Wednesday, April 06, Welcome to C++ Basic program style and I/O Class Creation Templates.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 16: Introduction to C++
OOP using C Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input.
Class and Structure. 2 Structure Declare using the keyword struct purpose is to group data Default visibility mode is public A struct doesn't have a constructor.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
C++ Class. © 2005 Pearson Addison-Wesley. All rights reserved 3-2 Abstract Data Types Figure 3.1 Isolated tasks: the implementation of task T does not.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Programming Language Principles Lecture 30 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Object-Oriented Programming.
1 CSE Programming in C++. 2 Overview Sign roster list Syllabus and Course Policies Introduction to C++ About Lab 1 Fill Questionnaire.
1 Introduction to Object Oriented Programming Chapter 10.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
1 n Object Oriented Programming. 2 Introduction n procedure-oriented programming consists of writing a list of instructions and organizing these instructions.
CS116 SENEM KUMOVA METİN. Outline What is C++ ? Some features of C++ Input and Output Operations Manipulators C++ header files Namespaces and scope resolution.
Prepared by Andrew Jung. Contents A Simple program – C++ C++ Standard Library & Header files Inline Functions References and Reference Parameters Empty.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
MAITRAYEE MUKERJI Object Oriented Programming in C++
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
Seventh step for Learning C++ Programming CLASS Declaration Public & Private Constructor & Destructor This pointer Inheritance.
Introduction to Programming
Introduction to C++ Systems Programming.
CS3340 – OOP and C++ L. Grewe.
This technique is Called “Divide and Conquer”.
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
Object Oriented Analysis and Design
Dynamic Memory Allocation Reference Variables
Object-Oriented Programming Part 1
Object-Oriented Programming
CLASSES AND OBJECTS.
CPS120: Introduction to Computer Science
C++ Programming CLASS This pointer Static Class Friend Class
Pointers, Dynamic Data, and Reference Types
Presentation transcript:

From C to C++

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

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;}

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”; }

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

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;}

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);

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;}

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)

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

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; }

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(); }

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

Inheritance Examples

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?? }

Polymorphism Polymorphism is the ability to use an operator or function in different ways Types of Polymorphism: Virtual functions Function name overloading Operator overloading

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?? }