1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.

Slides:



Advertisements
Similar presentations
Objects and Classes Part II
Advertisements

1 CSC241: Object Oriented Programming Lecture No 21.
EC-241 Object-Oriented Programming
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Classes: A Deeper Look Systems Programming.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
1 CSC241: Object Oriented Programming Lecture No 07.
Review of C++ Programming Part II Sheng-Fang Huang.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
1 CSC241: Object Oriented Programming Lecture No 13.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
More about Class 靜宜大學資工系 蔡奇偉副教授 ©2011. 大綱 Instance Class Members Class members can be associated with an instance of the class or with the class as a.
C++ Review (3) Structs, Classes, Data Abstraction.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
1 CSC241: Object Oriented Programming Lecture No 06.
Chapter 10 Introduction to Classes
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Computer Science Department CPS 235 Object Oriented Programming Paradigm Lecturer Aisha Khalid Khan Operator Overloading.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
More C++ Features True object initialisation
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn.
Class Miscellanea Details About Classes. Review We’ve seen that a class has two sections: class Temperature { public: //... public members private: //...
Structures, Classes and Objects Handling data and objects Unit - 03.
1 CSC241: Object Oriented Programming Lecture No 02.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
1 CSC241: Object Oriented Programming Lecture No 11.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
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.
CONSTRUCTOR AND DESTRUCTORS
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
1 CSC241: Object Oriented Programming Lecture No 05.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
More about Java Classes Writing your own Java Classes More about constructors and creating objects.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
1 CSC241: Object Oriented Programming Lecture No 03.
Structs and Classes Structs A struct can be used to define a data structure type as follows: struct Complex { double real, imag;} // specifying a Complex.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
CONSTRUCTOR AND DESTRUCTOR. Topics to be discussed……………….. 1. Introduction Introduction 2. FeaturesFeatures 3. Types of ConstructorTypes of Constructor.
1 CSC241: Object Oriented Programming Lecture No 08.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
Learners Support Publications Constructors and Destructors.
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
C++ Features Function Overloading Default Functions arguments Thinking about objects – relationship to classes Types of member functions Constructor and.
MAITRAYEE MUKERJI Object Oriented Programming in C++
Constructors and Destructors
Pointers and Dynamic Arrays
Visit for more Learning Resources
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Static data members Constructors and Destructors
CSC241: Object Oriented Programming
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
FUNCTIONS& FUNCTIONS OVERLOADING
Constructors and Destructors
Object Oriented Programming Using C++
Constructor, Destructor Mehroz Sadiq CS Faculty, UMT Department of CS&E, UET.
Constructors and Deconstructor
Objects as Function Arguments
Functions Chapter No. 5.
Presentation transcript:

1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh

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

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.

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

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

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

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

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

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.

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

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

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

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

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

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

Classes, Objects and Memory 16

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

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