Lecture 3 (UNIT -1) SUNIL KUMAR CIT-UPES.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Learning Objectives Structures Structure types Structures.
Structure.
Classes and Objects Presented by: Gunjan Chhabra.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
C++ fundamentals.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Structures  2 nd aggregate data type: struct  Recall:
Chapter 5 Classes and Objects §5.1 Specifying a class §5.2 Defining Member Functions §5.3 Memory Allocation and Static Members §5.4 Passing Objects to/from.
Learners Support Publications Classes and Objects.
Chapter 10 Introduction to Classes
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
 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.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.
CSC241 Object-Oriented Programming (OOP) Lecture No. 4.
STRUCTURE OF A C++ PROGRAM. It is a common practice to organize a program into three separate files. The class declarations are placed in a header file.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
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.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
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?
Function Overloading and References
Course Title Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 07 classes 1 BY ADEEL ANJUM ( MSc - cs, CCNA, WEB DEVELOPER )
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
CPS120: Introduction to Computer Science Lecture 16 Data Structures, OOP & Advanced Strings.
Slide 1 Chapter 6 Structures and Classes. Slide 2 Learning Objectives  Structures  Structure types  Structures as function arguments  Initializing.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Object Oriented Programming(Objects& Class) Classes are an expanded concept of data structures: like.
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Classes  Constructors  Principles of OOP  Class type member.
Class & Objects C++ offers another user-defined data type known class which is the most important feature of the object-oriented programming. A class can.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
1 n Object Oriented Programming. 2 Introduction n procedure-oriented programming consists of writing a list of instructions and organizing these instructions.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Structures and Classes
Programming with ANSI C ++
2 Chapter Classes & Objects.
Class and Objects UNIT II.
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?
10.2 Classes Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
Objectives Identify the built-in data types in C++
Review: Two Programming Paradigms
Chapter 3: Using Methods, Classes, and Objects
Classes & Objects.
Concepts and Basics of C++ Programming
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?
More about OOP and ADTs Classes
This technique is Called “Divide and Conquer”.
Student Book An Introduction
Lecture 1 Introduction.
Concepts and Basics of C++ Programming
Lecture 4-7 Classes and Objects
Subject Name: Object Oriented Programming with C++
CS212: Object Oriented Analysis and Design
More about OOP and ADTs Classes
Review for Final Exam.
Object-Oriented Programming Using C++ Second Edition
Learning Objectives Classes Constructors Principles of OOP
Dr. Bhargavi Dept of CS CHRIST
Classes and Objects.
CLASSES AND OBJECTS.
CPS120: Introduction to Computer Science
Review for Final Exam.
Submitted By : Veenu Saini Lecturer (IT)
Classes and Objects Systems Programming.
Presentation transcript:

Lecture 3 (UNIT -1) SUNIL KUMAR CIT-UPES

Objectives Upon completion of today lecture, You will be :- Able to understand the basic concept of object oriented programming. Able to understand Class , Objects, Class Members, Access Control, Parameter Passing. Able to use function inside and outside the class. Able to understand the functions with Parameter Passing

Structures ( revision) Keyword Name of Structure A way to combine different types of data into a single unit with continuous memory location Tool for handling group of logically related data items struct student { char name[20]; int roll_num; three fields with diff. data types- float total_marks; structure members or elements }; Three fields with diff. data types-structure members or elements

Structures Revised Keyword Name of Structure Structure name (student) can be used to create variable of type student. Example: struct student A; // C declaration Dot opertaor is used for accessing member variables as: strcpy(A.name, “John”); A.roll_num =25; A.total_marks =550; final_result = A.total_marks + 20; Structures can have array, pointers or structures as members. Declaration of a structure type variable Assigning value to member variables

Structures Revised Limitations of C structures: Struct data type can not be treated as built-in types, as shown: struct sum { float x; float y; }; struct sum a, b, c; Values can easily be assigned to a, b, c using dot operator but we can not add or subtract one from other, as shown: a= b+c; // illegal in C Secondly, no data hiding in structures i.e. structure members can be directly accessed by other structure variables by any function anywhere in their scope. Means structure members are public members.

Extensions to Structure C++ supports all the features of structures in C but expanded its capabilities to suits OOP philosophy User-defined data types to be treated as close as possible to the built-in data types Feature of DATA HIDING (one of the main principles of OOP) In C++, both variables and functions be used as members Some members can be declared as private so that can not be accessed by others directly. Keyword struct can be omitted in the declaration of structure variable, example: student A; // legal in C++ but illegal in C

Extensions to Structure C++ includes all these extensions in another user-defined type called class. Small syntactical difference b/w structures and classes in C++, hence one can use both interchangeably with minor modifications. Most of the programmers use structures for holding data and classes for both data and functions. The only difference between structure and class in C++ is that, by default, members of class is private while public in case of structure.

Class declaration- describes the type and scope of its members A way to bind data and functions together and allows data to be hidden, if required, from external use While defining class, we are creating a new abstract data type that can be treated as any other built-in data type. Two parts of class specification: Class declaration- describes the type and scope of its members Class function definitions- describes how the class functions are implemented.

General declaration of Class Keyword General declaration of Class Name of a Class class class_name { private: variable declarations; function declarations; public: }; Data hiding Access Specifier Class members

Class Variable declared inside class are- data members and function are- member functions. Only member functions can have access to the private data members and private functions Binding of data and functions together into a single class-type variable is referred to as encapsulation.

Class Simple example: class item { int number; //variables declaration float cost; //private by default public: void getdata(int a, float b); // function declaration void putdata(void); };

Object Creation item z; // memory for z is created Remember- class declaration does not define any object of class but only specifies what they contain Once class is declared, we can create variable of that type by using the class name, say item z; // memory for z is created Here the class variables are known as objects, so z is object of type item One can declare more than one object in one statement, say- item a, b, c; Also possible way, like in structures, for creating objects is: Class item { ……. } a, b, c;

Accessing Class Members The main() cannot contain statements that access data members directly. They can only be accessed through member functions. Syntax for accessing class members- object-name.function-name(actual-arguments); Example: x.getdata(500, 42.5); // is valid for object z Similarly- x.putdata(); //would display values

Accessing Class Members Invalid statements- getdata(100, 42.5); x.number= 300; //such statements have no meaning Variable declared as public can be accessed by objects directly, as shown: class xyz { int x; int y; public: int z; }; ……… xyz p; p.x =10; //error, x is private p.z =20 // accepted, z is public, should be avoided (defeats idea of data hiding) …........

Defining Member function Two places: Outside the class definition Inside the class definition Irrespective of the place of function definition, the function should perform the same task.

Outside the Class Definition Difference b/w member function and normal function is that a member function incorporates a membership ‘identical label’ in the header. This ‘label’ tells the compiler which class the function belongs to. Member function definition: return-type class-name :: function-name (argument declaration) { Function body }

Outside the Class Definition :: scope resolution operator, tells the compiler that the function function-name belongs to the class class-name. This means, scope of the function is restricted to the class-name specified in the header line. Void item :: getdata( int a, float b) { Number = a; Cost = b; } Void item :: putdata(void) Cout<< “number :”<< number << “\n”; Cout<< “cost :”<< cost << “\n”; }

Outside the Class Definition Characteristics of member functions: Several different classes can use the same function name. Member functions can access the private data of the class. A member function can call another member function directly, without using the dot operator.

Inside the Class Definition Example: class item { int number; Float cost; Public: Void getdata(int a, float b); Void putdata(void) //treated as inline function Cout << number << “\n”; Cout << cost << “\n”; } };

Inside the Class Definition Function defined inside class is treated as inline function. Hence all restrictions and limitations that apply to an inline function are also applicable here. Only small functions are defined inside the class definition.

C++ with class Int number; Float cost; Public: # include <iostream.h> Class item { Int number; Float cost; Public: Void getdata(int a, float b); Void putdata(void) Cout<<“number :” <<number<< “\n”; Cout<<“cost :” <<cost<< “\n”; } }; Void item :: getdata(int a, float b) Number=a; Cost=b;

C++ with class int main () { item x; cout<< “\nobject x” << “\n”; x.getdata (200, 140.55); x.putdata(); item y; cout<< “\nobject y” << “\n”; y.getdata (700, 159.32); y.putdata(); return 0; }

# include<iostream.h> class set { int m, n; public: When a member function can be called by using its name inside another member function of the same class, it is known as nesting of member function. # include<iostream.h> class set { int m, n; public: void input (void); void display (void); int largest(void); }; int set:: largest (void) { if (m >= n) return (m); else return (n); } void set : : input (void) cout << “input values of m & n:”;

Nesting of member functions int set:: largest (void) { if (m >= n) return (m); else return (n); } void set : : input (void) cout << “input values of m & n:”; cin >> m >> n; void set :: display(void) { cout << “largest value = “ <<largest(); //calling member function } main() set a a.input(); a.display();