Object Oriented Programming C++ Lecture No 4
Outline
- Some C++ Terminology
- Data Hiding
- Access Specifiers
- Objects as Function Arguments
- Returning Objects from Functions
- Classes vs. Structures
- Classes, Objects, & Memory
Some C++ Terminology
- A class is a grouping of data and functions.
- A class is very much like a structure type, it is only a pattern to be used to create a variable which can be manipulated in a program.
- An object is an instance of a class, which is similar to a variable defined as an instance of a type.
- A method (member function) is a function contained within the class.
- A message is the same thing as a function call. In object oriented programming, we send messages instead of calling functions.
A Class Acts as a Blueprint for New Objects
Data Hiding
- Data is concealed within a class, so that it can’t be accessed mistakenly by functions outside the class.
- The primary mechanism for hiding data is to put it in a class and make it private.
- Hiding data from parts of the program that don’t need to access it.
- One class’s data is hidden from other classes.
Access Specifiers
- private data or functions
– Can only be accessed from within the class. - public data or functions
– Are accessible from any where (both inside and outside the class).
Private Access Specifiers Ex:
Public Access Specifiers Ex:
Private and Public
- Functions are public
– Usually functions are public.
– This is a result of how classes are used.
– Functions that operate on data are public so they can be accessed from outside the class. - Data is private
– Data is hidden so it will be safe from accidental manipulation. - There is no rule that data must be private and functions public.
Objects as Function Arguments
1 2 3 4 5 6 7 8 9 10 11 12 13 |
//distance.h class Distance { private: int feet; float inches; public: void setFeet(int); void setInches(float); int getFeet(); float getInches(); void addDistance(Distance,Distance); }; |
distance.cpp file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
//distance.cpp #include"distance.h" void Distance::setFeet(int f) {feet=f;} void Distance::setInches( float i) {inches=i;} int Distance::getFeet() {return feet;} float Distance::getInches() {return inches;} void Distance::addDistance (Distance d1, Distance d2) {inches=d1.inches+d2.inches; feet=0; if(inches>=12.0) {inches-=12.0; feet++;} feet+=d1.feet+d2.feet; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
//driver.cpp #include"distance.h" #include<iostream> using namespace std; void main() { Distance dist1, dist2, dist3; dist1.setFeet(11); dist1.setInches(6.25); dist2.setFeet(17); dist2.setInches(5.75); dist3.addDistance (dist1,dist2); cout<<dist3.getFeet() <<endl; cout<<dist3.getInches() <<endl; } |
Output
29
0
Press any key to continue
Objects as Function Arguments
- Every call to a member function is associated with a particular object.
- The function has direct access using the member names alone to all the members, whether private or public, of that object.
– Example: feet and inches - It also has indirect access, using the object name and the member name, connected with the dot operator to other objects of the same class that are passed as arguments.
– Example: d1.inches or d2.feet
Structures vs. Classes
- We believe that
– Structures provide a way to group data
– Classes provide a way to group both data and functions. - In fact, structures can be used in almost exactly the same way as classes.
- The only formal difference remains that
– In a class, members are private by default.
– In a structure, members are public by default.
Part Example (with class)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#include<iostream> using namespace std; class Part { int modelno; int partno; float cost; public: void setPart(int mn, int pn, float c) { modelno=mn; partno=pn; cost=c; } void showPart() { cout<<"Model : "<<modelno<<endl; cout<<"Part : "<<partno<<endl; cout<<"Cost : "<<cost<<endl; } }; void main() { Part part; part.setPart(555,100,100); part.showPart(); } |
Part Example (with struct)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#include<iostream> using namespace std; struct Part { Private: int modelno; int partno; float cost; public: void setPart(int mn, int pn, float c) { modelno=mn; partno=pn; cost=c; } void showPart() { cout<<"Model : "<<modelno<<endl; cout<<"Part : "<<partno<<endl; cout<<"Cost : "<<cost<<endl; } }; void main() { Part part; part.setPart(555,100,100); part.showPart(); } |
Classes, Objects, and Memory
- We believe
– Each object created from a class contains separate copies of that class’s data and member functions.
– It emphasizes that objects are complete, self-contained entities, designed using the class declarations.
– Example: cars - In fact
– Each object has its own separate data items.
– On the other hand, all the objects in a given class use the same member functions.
– The member functions are created and placed in memory only once, when they are defined in the class declaration.
– The rational being: functions for each object are identical.