Download presentation
Presentation is loading. Please wait.
Published byMelvyn Wells Modified over 9 years ago
1
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh
2
C++ Objects as Data types C++ objects are used to define variables of user-defined data types. User can the type of data in the class and assign values to variables according to define set of values, hence objects are helping in creating new data types. 2
3
C++ Objects as Data types (distance example) //cl_englObj.cpp #include class distance { private: int feet; float inches; public: void setdist(int ft, float in) { feet=ft; inches=in; } void getdist() { cout >feet; cout >inches; } void showdist() //display distance { cout<<feet<<"\'-"<<inches<<'\"'; } }; int main() { distance dist1, dist2; //define two lengths dist1.setdist(11,6.25); //set distance 1 dist2.getdist(); //get dist2 from user display lengths cout<<"\n dist1= ";dist1.showdist(); cout<<"\n dist2= ";dist2.showdist(); cout<<endl; } 3 Program: Write a program using classes, which could get feet and inches from user and display in main functions with specific format.
4
C++ Objects as Data types (distance example) Three are three member functions used: Setdist() Use argument to set feet and inches Getdist() Get value of feet and inches from user Showdist() Displays distance in feet and inches There are two objects dist1 Provided by fixed arguments Dist2 Provided by user input 4
5
Functions (Private or Public) Member functions may be private or public. Member need to work outside the class are set as a public. Members may be private. 5
6
Constructors A constructor is a member function that is automatically invoked when a new object of a class is instantiated. The constructor must always have the same name as the class and has no return type. Multiple constructors for the same class can be defined. If multiple constructors are used in a single class then they must have different parameters called as overloading. Constructor must be declared as a public. 6
7
Constructors (Contd.) Constructor is either user defined or automatically added by c++ compiler. Note: Default constructor will not perform the function of automatic initialization. Note: Main purpose of constructor is to initialize or assign known state of data members of class, automatically. 7
8
Constructors (Contd.) (return statement) return 0; This statement inform operating system that the program executed without errors. If program executed incorrectly it will return any other integer. If we are using void before main function, then there is no need to write return 0; statement at the end of program, because void keyword will perform same function. if (program_executed_fine) return 0; else if (program_had_error) return 1; 8
9
Constructor (auto-initialization program) //Cl_counter.cpp #include class Counter { private: unsigned int count; public: Counter(): count(0)//constructor {/*emptly body*/} void inc_count() {count++;} int get_count() {return count;} }; //Counter() is constructor and count is its variable Int main() { Counter c1,c2; //Define & Initialize to 0 cout<<"\n c1= "<<c1.get_count(); //display cout<<"\n c2= "<<c2.get_count(); c1.inc_count(); //increment c1 c2.inc_count(); //increment c2 cout<<"\n c1= "<<c1.get_count(); //display again cout<<"\n c2= "<<c2.get_count(); cout<<endl; } 9 Program: Write a program using class, which uses constructor and show automatic initialization of variables.
10
Constructor (auto-initialization program) In auto-initialization program, count is data member. It has three member functions: Counter() inc_count() get_count() Auto-initialization: constructor is used for auto-initialization. Counter c1, c2; Counter() is a constructor, in above statement, constructor is executed which not only create to objects but set their initial values to 0. 10
11
Constructor (auto-initialization program) Features of Constructor 1. Compiler will recognize constructor by name, because it has same name as the name of class. 2. Constructor has no return type hence it does not return anything. Initialization: Initialization of constructor is performed in: count() : count(0) { } Which is equal to: count() { count = 0; } // inappropriate because every counter c1 is defined we need to initialize count = 0; 11
12
Destructors Destructor: It is a function called automatically when an object is destroyed. The name of destructor is same as the name of class, by is preceded by a tilde (~). Destructors also have no return type. Destructors do not take any argument. Purpose of Destructors: It de-allocate memory that was allocated for the object by constructor. 12
13
Structures and Classes Structures are used to group data. Classes are used to group data and functions. In structure members are by default public. In class members are by default private. 13
14
Structures and Classes // example of structures #include struct original { int a; float b; double c; char d; string mystring; }; //example of class class Date { private: //private in small letters int day; int month; int year; public: void set_month(int day,int month, int year) { cout<<day<<"-"<<month<<"-"<<year; } }; 14
15
Classes, Objects and Memory Separate data but same member function Separate Data: All the objects have separate data items because data of each object is different from other object. Same Member Function: All the objects are using same member function in the class, placed in memory only once, because functions are same for all objects. 15
16
Classes, Objects and Memory 16
17
Static Class Data If the data item in a class is declared as static, that item is created only once for whole class (for all objects). It is useful when all objects of the same class must share a common item of information. It is visible only within class, but lifetime is the entire program. Purpose of static class data: It is used when object needed to know how many other objects of its class were in the program. Example: In a road-racing game, a race car might want to know how many other cars are still in the race. 17
18
Constant and classes Constant is used with normal variables to prevent them from being modified (changed). const keyword is used before variable data type. const int size = 6; Const can be used with function arguments to keep a function from modifying a variable passed to it by reference. 18
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.