Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object Oriented Programming Using C++

Similar presentations


Presentation on theme: "Object Oriented Programming Using C++"— Presentation transcript:

1 Object Oriented Programming Using C++
Anbreen Kausar

2 Outline Returning Objects from function Class Objects& Memory
Static Data Members Static Class Data Static Data Members & Ordinary Data Members Static Data Declaration&Definition Static Member Functions Constant Member Functions Constant Objects Overloaded Functions

3 Returning Objects from Function
class Distance { private: int feet; float inches; public: Distance() : feet(0), inches(0) { } Distance(int ft, float in): feet(ft), inches(in) { } void getdist() { cout << "\nEnter feet: "; cin >> feet; cout << "\nEnter inches: "; cin >> inches; } void showdist() { cout << feet << "ft " << inches << "inches\n";} Distance add_distance(Distance); };

4 Returning Objects from Function
Distance Distance::add_dist(Distance d1) // function returns value of type Distance { Distance temp; temp.inches = inches + d1.inches; if(temp.inches >=12.0) { temp.inches-=12.0; temp.feet++; } temp.feet = feet + d1.feet; return temp; }

5 Classes, Objects, and Memory
Each object created from a class contains separate copies of that class’s data and member functions. The member functions are created and placed in memory only once—when they are defined in the class declaration The functions for each object are identical. The data items, however, will hold different values, so there must be a separate instance of each data item for each object. Data is therefore placed in memory when each object is defined, so there is a separate set of data for each object.

6

7 Static Data Members If a data item in a class is declared as static, then only one such item is created for the entire class, no matter how many objects there are Static class data is used to share information among the objects of a class Associated with the class itself rather than with any class object, they are also known as class variables

8 Static Class Data: Example
We have a class of racing cars in a racing game Every car needs to know the number of other cars currently on track Use static data item to count number of cars

9 Static Data Members: Difference
Static data member is different from ordinary data members of a class in various ways: There is only one copy of a static data member maintained for the entire class which is shared by all the objects of that class. It is visible only within the class; however, its lifetime (the time for which it remains in the memory) is the entire program.

10 Static Data: Declaration & Definition
Two things are needed for making a data member static: Declaration within the class definition Definition outside the class definition Why !?! If static member is defined inside the class declaration, it would violate the idea that a class declaration is only a template and does not set aside any memory. Memory space for such data is allocated only once, before the program starts to execute Static member is more like a global variable, same for all objects of the class

11 Static Data: Declaration & Definition
Declaration starts with the keyword static class Counter {       static int count ; // declaration within the class       ... } The definition, of above declared static member count, outside the class will be as follows: int Counter::count = 0; // definition outside the class

12 Static Member Functions
Function that accesses only the static members of a class Similar to static data members they do not belong to any object Keyword static before function declaration in the class definition class Counter {       static int count ;       static void display() {          cout << count << "n";      } }; int Counter::count=0;

13 Static Member Functions
By just prefixing the keyword static before the function declaration in the class definition, you have declared the function as static. Can access private and public data of class, but An object has to be passed as an argument for it to access members otherwise it makes no sense! static int total_inches(Distance d1){ return (d1.feet*12 + d1.inches;)} Invoked by using the class name instead of its objects Counter.display( );

14 const Member Functions
A const member function guarantees that it will never modify any of its class’s member data Where is the most use for const member functions? Member functions that do nothing but display object data are obvious members for being made const.

15 const Member Functions
class Counter {       static int count ;       void inc_counter(){ count++; } void display() const {          cout << count << "n"; }; If there is separate definition const must be used both in declaration and definition

16 const Objects const objects can only use const member functions
Since const member functions cannot modify any data therefore const objects can also not modify any class data When you are designing classes it’s a good idea to make a function const if it does not modify any of its object’s data

17 Example: const class Distance { private: int feet; float inches;
public: Distance(int ft, float in) : feet(ft), inches(in) {} void getdist(); void showdist() const { cout << feet << “ft “ << inches << “in”;} };

18 Example: const int main() { const Distance dist1(300,0);
//dist1.getdist(); //Error: getdist() not const cout << “dist1 = “; dist1.showdist(); cout << endl; return 0; }

19 Overloaded Functions Functions can be overloaded to perform different operations, depending upon the function call Overloading can be done using different number of arguments, or even different types of arguments For example; void function(); void function(int a); void function(int a, int b); void function(float a, float b);

20 Example Output Here is int 10 Here is float 10.1 Here is char* ten
#include <iostream> using namespace std; void print(int i) { cout << " Here is int " << i << endl; } void print(double f) { cout << " Here is float " << f << endl; void print(char* c) { cout << " Here is char* " << c << endl; int main() { print(10); print(10.10); print("ten"); Output Here is int 10 Here is float 10.1 Here is char* ten


Download ppt "Object Oriented Programming Using C++"

Similar presentations


Ads by Google