1 / 186

OBJECT ORIENTED PROGRAMMING WITH C++

OBJECT ORIENTED PROGRAMMING WITH C++. B.R.MOHAN CSE DEPT SSE, MUKKA.

Télécharger la présentation

OBJECT ORIENTED PROGRAMMING WITH C++

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Prof. B.R.Mohan, SSE--> www.bookspar.com | Website for students | VTU NOTES OBJECT ORIENTED PROGRAMMING WITH C++ B.R.MOHAN CSE DEPT SSE, MUKKA

  2. Prof. B.R.Mohan, SSE--> www.bookspar.com | Website for students | VTU NOTES Text Book 1. Object oriented Programming with C++, Sourav Sahay, Oxford Press, 2006 (chapters1-10) Reference Books1. C++ Primer, Stanley Lipmann, Josee Lajoie, Barbara E. Moo, 4th Edition, Addison Wesley, 20052. The Complete Reference C++, Herbert Schildt, 4th Edition, TMH, 2005

  3. Prof. B.R.Mohan, SSE--> www.bookspar.com | Website for students | VTU NOTES Unit-1: 1. Introduction to C++2. Class and Object Unit-2: • Class & Objects contd. Unit-3: 4. Dynamic Memory Management 5. Constructors and Destructors

  4. Prof. B.R.Mohan, SSE--> www.bookspar.com | Website for students | VTU NOTES Unit-4:6. Inheritance Unit-5: • Virtual Functions & Dynamic Polymorphism • Stream Handling Unit-6 • Stream Handling contd 10. Operator Overloading

  5. Prof. B.R.Mohan, SSE--> www.bookspar.com | Website for students | VTU NOTES Unit-7:11. Operator Overloading contd. Unit-8: 12. Type Conversion, New style casts, and RTTI 13. Templates FT,CT, STL 14. Exception Handling

  6. Prof. B.R.Mohan, SSE--> www.bookspar.com | Website for students | VTU NOTES Here we see how to program in C++ - a language that support object oriented programming. Reference – Robert Lafore Key concepts in OOPs – Objects and Classes Oops came into existence due to the limitations discovered in procedural languages.

  7. Prof. B.R.Mohan, SSE--> www.bookspar.com | Website for students | VTU NOTES Procedural Languages – Pascal, C, Basic, Fortran are examples of procedural languages. lays emphasis on executing a set of instructions. -Get some input -Add these numbers -divide by 6 -display the results

  8. Prof. B.R.Mohan, SSE--> www.bookspar.com | Website for students | VTU NOTES • A program in procedural language is a list of instructions.For small programs, no other organizing principle is needed. • As program grows larger, it becomes difficult to comprehend. Hence Divide the large program into a number of functions. A Function has a clearly defined purpose and a well defined interface.

  9. Prof. B.R.Mohan, SSE--> www.bookspar.com | Website for students | VTU NOTES • Dividing a function into a number of functions can be extended to grouping a number of functions into a larger entity called Module. • Dividing a program into functions and modules is one of the cornerstone of Structured Programming( other features include loops and other control structures). 3. As program grow even larger , structured programming signs of strain.

  10. Prof. B.R.Mohan, SSE--> www.bookspar.com | Website for students | VTU NOTES Drawbacks/Disadvantages of Procedural languages. • Data is undervalued – given II class status in program organization of procedural languages. • Since many functions in a program can access global data / global variables, global data can be corrupted by that have no business to change it. Global variables constitute data are declared outside any function so that they are accessible to all functions

  11. Prof. B.R.Mohan, SSE--> www.bookspar.com | Website for students | VTU NOTES Since many functions access the same data, the way data is arranged becomes critical. • Data arrangement cannot be changed without modifying the functions that access it. If u add new data items , we need to modify all the functions that access the data so that they can also access these new items

  12. Prof. B.R.Mohan, SSE--> www.bookspar.com | Website for students | VTU NOTES Global Variable Accessible by any function Accessible by Function A Accessible by Function B Local Var Local Var Function A Function B

  13. Prof. B.R.Mohan, SSE--> www.bookspar.com | Website for students | VTU NOTES GlobalData Global Data Global Data Global Data Fn A Fn A Fn A Fn A

  14. Prof. B.R.Mohan, SSE--> www.bookspar.com | Website for students | VTU NOTES Hence we need a way to restrict the access to the data , to hide it from all but a few critical functions. This protects the data, simplifies the maintainence and other benefits. Object Oriented Approach Idea of Object Oriented Language – data and the functions that access the data are combined into a single unit called OBJECT. An objects functions are called Member functions in C++. MFs provide only way to access data.

  15. Prof. B.R.Mohan, SSE--> www.bookspar.com | Website for students | VTU NOTES How to read a data item in an object ? • Call the mf of the object and it will read the data item and return the value to u. • Data cant be accessed directly, it is hidden within the object and safe from accidental alterations. • Data and functions are said to be encapsulated in an object. • Data Encapsulation & Data Hiding are the key terms in OOPs

  16. Prof. B.R.Mohan, SSE--> www.bookspar.com | Website for students | VTU NOTES • OOPs simplify writing , debugging and maintaining the program. • C++ prg typically contain a number of objects interacting with each other by calling one another’s member functions.

  17. Prof. B.R.Mohan, SSE--> www.bookspar.com | Website for students | VTU NOTES Organization Of a C++ ProgramFig – Object Oriented Paradigm object Data MF MF Object Object Data Data MF MF MF MF

  18. Prof. B.R.Mohan, SSE--> www.bookspar.com | Website for students | VTU NOTES Corporate Paradigm Sales Data CSE Dept Sales Manager Secretary Finance Dept Personnel Dept Personnel Data Finance Data Personnel Manager Finance Manager Personnel Staff Financial Assistant

  19. Prof. B.R.Mohan, SSE--> www.bookspar.com | Website for students | VTU NOTES There is a close match between programming sense & objects in real world Classes – Objects are instances / members of a class. Eg- All programming languages have built in data types like int, char, float,etc. Similarly u can have objects of same class as shown

  20. Prof. B.R.Mohan, SSE--> www.bookspar.com | Website for students | VTU NOTES A Class serves as a plan / template like structure, specifies what data & functions will be included in objects of the class. Fig- A Class & its Objects Object1 Object2 Object3 Class Circle Feature A Feature B Feature C Specifications for class Circle Class Circle Feature A Feature B Feature C Class Circle Feature A Feature B Feature C Class Circle Feature A Feature B Feature C

  21. Prof. B.R.Mohan, SSE--> www.bookspar.com | Website for students | VTU NOTES Object1, Object2 and Object3 are all objects of a Class Circle that are similar to i, j, k are of integer data type in C language. Saimple programs are given in VC++ D:\

  22. Prof. B.R.Mohan, SSE--> www.bookspar.com | Website for students | VTU NOTES C++ Program execution Execution starts from main( ). smallobj s1,s2; Defines 2 objects s1,s2, of class smallobj. Class smallobj doesn’t create any objects , but describes how they look when they are created. Space is set aside for it in memory. S1.setdata(1066) ; //causes somedata set to 1066 . -> class member acess operator connects object name and member function

  23. Prof. B.R.Mohan, SSE--> www.bookspar.com | Website for students | VTU NOTES C++ introduces a new keyword class as a substitute for keyword struct . structure members are public by default

  24. Prof. B.R.Mohan, SSE--> www.bookspar.com | Website for students | VTU NOTES Struct Distance { private: int iFeet; float fInches; Public: void setFeet(int x) { iFeet=x; } float getFeet() { return iFeet; } int setInches(float y) { fInches = y; } float getInches() { return fInches; } }; This can also be written as given in next slide

  25. Prof. B.R.Mohan, SSE--> www.bookspar.com | Website for students | VTU NOTES Struct Distance { private: int iFeet; float fInches; void setFeet(int x) //All member functions are public by default { iFeet=x; } float getFeet() { return iFeet; } int setInches(float y) { fInches = y; } float getInches() { return fInches; } };

  26. Prof. B.R.Mohan, SSE--> www.bookspar.com | Website for students | VTU NOTES class members are private by default

  27. Prof. B.R.Mohan, SSE--> www.bookspar.com | Website for students | VTU NOTES class Distance { Private: int iFeet; float fInches; Public: void setFeet(int x) { iFeet=x; } float getFeet() { return iFeet; } int setInches(float y) { fInches = y; } float getInches() { return fInches; } }; class members are private by default

  28. Prof. B.R.Mohan, SSE--> www.bookspar.com | Website for students | VTU NOTES class Distance { int iFeet; //private by default float fInches; //private by default Public: void setFeet(int x) { iFeet=x; } float getFeet() { return iFeet; } int setInches(float y) { fInches = y; } float getInches() { return fInches; } };

  29. Prof. B.R.Mohan, SSE--> www.bookspar.com | Website for students | VTU NOTES Classes and Objects Classes contain Data and Functions Data Function Class Data1 Data2 Data3 Fn1 Fn2 Fn3

  30. Prof. B.R.Mohan, SSE--> www.bookspar.com | Website for students | VTU NOTES Class can contain private and public.Usually the data within the class is private and the functions that operate on data are public so that they can be accessed from outside the class. Fig- for public & private Class Not accessible From outside Class Accessible from Outside class public private Data or Functions Data or Functions

  31. Prof. B.R.Mohan, SSE--> www.bookspar.com | Website for students | VTU NOTES Syntax of a Class Specifier Class Circle name of class { class is a keyword private: int data; private functions & data public: void DisplayRadius( ); void CalcArea( ); public functions & data void CalcCircum( ); };

  32. Prof. B.R.Mohan, SSE--> www.bookspar.com | Website for students | VTU NOTES Differences between C and C++ In C, u may / may not In C++, you must Include function include function Prototypes prototypes. C++ lets you specify default values for function‘s parameters

  33. Prof. B.R.Mohan, SSE--> www.bookspar.com | Website for students | VTU NOTES C++ lets you specify default values for function‘s parameters in the function’s protoype

  34. Prof. B.R.Mohan, SSE--> www.bookspar.com | Website for students | VTU NOTES Eg-void myfunction(int x=3,int y=4) In C, the declaration In C++, u may Of a variable must place the be at the beginning variable declara Of the function tions close to the statements that use variables before using it in a statement

  35. Prof. B.R.Mohan, SSE--> www.bookspar.com | Website for students | VTU NOTES If a C program uses a In C++, u can Local variable that has instruct prgm Same name as global to use value of Variable, then C program global variable uses the value of a with scope local variable. Resolution operator Eg- cout << “Iam global var : ” << ::I;

  36. Prof. B.R.Mohan, SSE--> www.bookspar.com | Website for students | VTU NOTES Lab Program-1 1. Given that Employee class contains following members: employeeno., empname, basic , da, it, Netsalary and to print data members. Write a C++ program to read the data of n employees and compute netsalary of each employee.(da=52%of basic and it =30% of gross salary, netsalary=basic+da-it)

  37. Prof. B.R.Mohan, SSE--> www.bookspar.com | Website for students | VTU NOTES • Steps : 1. Define an Employee class and declare the given data members. 2. Get the employee details using the member function 3. calculate netsalary of given employee using formula display the resultant value 4. write the main function and create Employee objects. Call member functions on the employee objects to read the data ,to calculate netsalary and to print the data members Class: • The class is a fundamental OOP concept in C++. • A class declaration defines a new type that links code(functions/operations) and data. • This new type is then used to declare objects of that class.

  38. Prof. B.R.Mohan, SSE--> www.bookspar.com | Website for students | VTU NOTES • The class is a fundamental OOP concept in C++. • A class declaration defines a new type that links code(functions/operations) and data. • This new type is then used to declare objects of that class. Hence , a class is a logical abstraction, but a physical existence.

  39. Prof. B.R.Mohan, SSE--> www.bookspar.com | Website for students | VTU NOTES Syntax: class name objectname object name.datamember; //access class members object name.datamember; //calling mf on an object Access specifiers: public: allows fns / data to be accessible to other parts of ur Prg private: by default fucntions and data declared within a class are private to that class and may be accessed only by public members of the same class.

  40. Prof. B.R.Mohan, SSE--> www.bookspar.com | Website for students | VTU NOTES Class definition follows Class Employee{ //declare data members here int empno; char empname[25]; float basic, da, it, netsal; public: void getdata(); void computenetsal(); void display(); };

  41. Prof. B.R.Mohan, SSE--> www.bookspar.com | Website for students | VTU NOTES Member functions void employee::getdata() { //get emp details} void employee::computenetsal() { da=0.52*basic; gross=basic-da; it=0.3*gross; //compute netsal netsal=basic+da-it; }

  42. Prof. B.R.Mohan, SSE--> www.bookspar.com | Website for students | VTU NOTES Void employee::display() { //display employee info on the monitor }

  43. Prof. B.R.Mohan, SSE--> www.bookspar.com | Website for students | VTU NOTES • Define a student class with Usn, name , marks in 3 tests of a subject. Declare an array of 10 student objects. Using appropriate functions, find average of 2 better marks for each student. Print usn, name and average marks of all students.

  44. Prof. B.R.Mohan, SSE--> www.bookspar.com | Website for students | VTU NOTES Steps For II Programs • Define a Student class and declare the given members. • Get the Student details using member functions to get data • Find the average of 2 better marks for each student • Display resultant values – usn, name and average marks of all students

  45. Prof. B.R.Mohan, SSE--> www.bookspar.com | Website for students | VTU NOTES 5. Write main() function and create an array of 10 student objects. Call member functions on student objects to read data to calculate average marks and to print the student details along with average marks.

  46. Prof. B.R.Mohan, SSE--> www.bookspar.com | Website for students | VTU NOTES Steps- • Define a student class and declare the given data members • Get the student details using member function- getdata() • Find average of 2 better marks for each student • Display the resultant values-usn, name and average marks of all students • Write a main() & create an array of 10 student objects. Call mfs of student objects to read data, to calculate average marks & print the student details along with their avg mks

  47. Prof. B.R.Mohan, SSE--> www.bookspar.com | Website for students | VTU NOTES Lab Program-3 • Write a C++ program to create a class called COMPLEX and implement following overloading functions ADD that return a COMPLEX number. i) ADD(a,s2) - where a is an integer (real part) and s2 is a complex number. ii) ADD(s1,s2) – where s1, s2 are complex numbers

  48. Prof. B.R.Mohan, SSE--> www.bookspar.com | Website for students | VTU NOTES Steps for LAB PRG III Constructor : 1. Create a user defined datatype named as COMPLEX. Declare the data members. 2. Initialize data members with default values. This is a default constructor automatically invoked when an object of type COMPLEX is defined / created.

  49. Prof. B.R.Mohan, SSE--> www.bookspar.com | Website for students | VTU NOTES 3. Or initialize the data members with default values specified while creating the object. This is a Parameterized constructor, automatically invoked when some initial values are passed as parameters while creating an object. • Add an integer with a COMPLEX value • Add 2 COMPLEX objects overloading the ADD function

  50. Prof. B.R.Mohan, SSE--> www.bookspar.com | Website for students | VTU NOTES • Display the results directly or by overloading << operator • Write a main() function and call member functions on the COMPLEX objects as required by the end user.

More Related