Lecture 4-7 Classes and Objects

Slides:



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

Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
Classes and Objects Presented by: Gunjan Chhabra.
Chapter 5 Functions.
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
1 Lab Session-XII CSIT121 Fall 2000 b Namespaces b Will This Program Compile ? b Master of Deceit b Lab Exercise 12-A b First Taste of Classes b Lab Exercise.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
Computer Science 1620 Lifetime & Scope. Variable Lifetime a variable's lifetime is finite Variable creation: memory is allocated to the variable occurs.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
Object Oriented Programming (OOPs) Class Object Data Hiding Abstraction Encapsulation Polymorphism Inheritance Difference between Procedural and OOPs programming.
C++ function call by value The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter.
Learners Support Publications Classes and Objects.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and 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.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
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.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
1 CSC241: Object Oriented Programming Lecture No 02.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
CPS120: Introduction to Computer Science Lecture 16 Data Structures, OOP & Advanced Strings.
1 Introduction to Object Oriented Programming Chapter 10.
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.
1 n Object Oriented Programming. 2 Introduction n procedure-oriented programming consists of writing a list of instructions and organizing these instructions.
1 Chapter 8 Scope, Lifetime, and More on Functions CS185/09 - Introduction to Programming Caldwell College.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
EGR 2261 Unit 13 Classes Read Malik, Chapter 10.
Eine By: Avinash Reddy 09/29/2016.
Lecture 3 (UNIT -1) SUNIL KUMAR CIT-UPES.
Structures and Classes
Classes (Part 1) Lecture 3
Classes C++ representation of an object
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?
C++ Templates.
Review: Two Programming Paradigms
Chapter 3: Using Methods, Classes, and Objects
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?
Chapter 5 Classes.
Object-Oriented Programming Using C++
Concepts and Basics of C++ Programming
Static Data Member and Functions
Subject Name: Object Oriented Programming with C++
Lecture 9 Concepts of Programming Languages
Abstract Data Types and Encapsulation Concepts
Lecture 8 – 9 Arrays with in a class
CS212: Object Oriented Analysis and Design
Separate Compilation and Namespaces
Chapter 5 Function Basics
Packages and Interfaces
6 Chapter Functions.
Learning Objectives Classes Constructors Principles of OOP
Dr. Bhargavi Dept of CS CHRIST
Classes and Objects.
Object Oriented Programming Using C++
Method of Classes Chapter 7, page 155 Lecture /4/6.
Submitted By : Veenu Saini Lecturer (IT)
CS410 – Software Engineering Lecture #5: C++ Basics III
Classes C++ representation of an object
CS1201: Programming Language 2
Instructor: Hidayah Elias Course: COMPUTER PROGRAMMING (BFC 2042)
Classes and Objects Systems Programming.
11.1 The Concept of Abstraction
Lecture 9 Concepts of Programming Languages
Introduction to Classes and Objects
Presentation transcript:

Lecture 4-7 Classes and Objects

Contents Introduction to Classes and Objects Class declaration Creating Objects Accessing Object Members Defining Member Functions Nesting of Member Function Access Specifiers: Public , Private Nested Classes

Classes Different from standard C structures Adds member functions Not just member data Integral to object-oriented programming Focus on objects Object: Contains data and operations In C++, variables of class type are objects

Specifying a Class A class is a way to bind the data and its associated functions together. A class specification has 2 parts: Class declaration Class function definitions Class declaration: describes type and scope of its members. Class function definitions: describes how the class functions are implemented.

General form of Class declaration

Difference between Structure and Class By default, members of class are private, while, by default, members of structure are public. Encapsulation The keywords public and private are known as visibility labels.

A Simple Class Example class item { }; Creating Objects: item x; int number; float cost; public : void getdata(int a, float b); void putdata (void); }; Class Representation Creating Objects: item x; Accessing Class Members: Object_name.function-name (actual-arguments); x.getdata(100, 75.5); x.putdata( );

Creating Objects Objects can also be created as follows:

Complete Example of a Class - 1 #include <iostream> using namespace std; class myclass { public: int i, j, k; // accessible to entire program }; int main() myclass a, b; a.i = 100; // access to i, j, and k is OK a.j = 4; a.k = a.i * a.j; b.k = 12; // remember, a.k and b.k are different cout << a.k << " " << b.k; return 0; }

Complete Example of a Class - 2 #include <iostream> using namespace std; class myclass { int a, b; public: void init(int i, int j) { a=i; b=j; } void show() { cout << a << " " << b << "\n"; } }; int main() myclass x; x.init(10, 20); x.show(); return 0; }

Memory Allocation for Objects Memory space for objects is allocated when they are declared and not when the class is specified : partially true. Since all the objects belonging to that class use the same member functions, no separate space is allocated for member functions when the objects are created. Only space for member variables is allocated separately for each object. Because, member variables will hold different data values for different objects.

Example

Member Functions

Defining Member Functions Outside the class definition When function is defined outside class, it requires a prototype declaration in the class. Inside the class definition When function is defined inside class, it does not require a prototype declaration in the class.

Inline Functions In C++, you can create short functions that are not actually called; rather, their code is expanded in line at the point of each invocation. This process is similar to using function-like macro. To cause a function to be expanded in line rather than called, precede its definition with the inline keyword.

Inline Functions We can define a member function outside the class definition and still make it inline by using the qualifier inline in the header line of the function definition.

Nesting of Member Functions

Nesting of Member Functions An object of a class using dot operator generally calls a member function of the class. However, a member function may also be called by using its name inside another member function of the same class. This is known as “Nesting of Member Functions”

Nesting of Member Functions

Access Specifiers

Data Hiding Revisited

Access Specifier: Public Public members are accessible outside the class. Public members are accessible to both member Functions and non-member Functions. Objects can access the members directly using dot operator. E.g. object name.public data member object name.public member function 

Access Specifier: Public

Access Specifier: Private Private Members can only be accessed by the member functions of that class. They cannot be accessed by any non member functions (data is hidden from outside world). Object of a class cannot access private members using dot operators. All data at the beginning of a class is by default private, until any specifier is mentioned.

Access Specifier: Private

Private Member Functions

Private Member Functions

Nested Classes

Nested Classes A class is declared within another class. The outer class is called enclosing class and inner class is called the nested class. A nested class is a member and as such has the same access rights as any other member. The members of an enclosing class have no special access to members of a nested class; the usual access rules shall be obeyed.

Nested Classes Example #include<iostream> using namespace std; class Enclosing /* start of Enclosing class declaration */ { int x; class Nested { /* start of Nested class declaration */ int y; void NestedFun(Enclosing e) cout<<e.x; // works fine: nested class can access // private members of Enclosing class } }; // declaration Nested class ends here }; // declaration Enclosing class ends here int main(){}

Nested Classes Example #include<iostream> using namespace std; class Enclosing /* start of Enclosing class declaration */ { int x; class Nested { /* start of Nested class declaration */ int y; }; // declaration Nested class ends here void EnclosingFun(Nested n) cout<<n.y; // Compiler Error: y is private in Nested } }; // declaration Enclosing class ends here int main(){}

Thank You