2 Chapter Classes & 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

Spring Semester 2013 Lecture 5
Classes and Objects Presented by: Gunjan Chhabra.
Road Map Introduction to object oriented programming. Classes
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
C++ for Engineers and Scientists Third Edition
Object Oriented Programming (OOPs) Class Object Data Hiding Abstraction Encapsulation Polymorphism Inheritance Difference between Procedural and OOPs programming.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
Chapter 6: Modularity Using Functions. In this chapter, you will learn about: – Function and parameter declarations – Returning a single value – Returning.
C++ for Engineers and Scientists Second Edition Chapter 6 Modularity Using Functions.
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.
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
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
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.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Objects and Classes.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
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++ Class. © 2005 Pearson Addison-Wesley. All rights reserved 3-2 Abstract Data Types Figure 3.1 Isolated tasks: the implementation of task T does not.
Course Title Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 07 classes 1 BY ADEEL ANJUM ( MSc - cs, CCNA, WEB DEVELOPER )
Programming in java Packages Access Protection Importing packages Java program structure Interfaces Why interface Defining interface Accessing impln thru.
+ Storage Classes and Linkage. + Introduction Scope describe the region or regions of a program that can access and identifier Variables can be shared.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Object Oriented Programming(Objects& Class) Classes are an expanded concept of data structures: like.
Programming Fundamentals Enumerations and Functions.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
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.
Learners Support Publications Constructors and Destructors.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
Classes in C++ By: Mr. Jacobs. Objectives  Explore the implications of permitting programmers to define their own data types and then present C++ mechanism.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Constructors and Destructors
Lecture 3 (UNIT -1) SUNIL KUMAR CIT-UPES.
Classes C++ representation of an object
Programming with ANSI C ++
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?
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.
Chapter 10-1: Structure.
Review: Two Programming Paradigms
Chapter 3: Using Methods, Classes, and Objects
Classes & 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.
Constructor & Destructor
This technique is Called “Divide and Conquer”.
User-Defined Functions
C++ for Engineers and Scientists Second Edition
Lecture 4-7 Classes and Objects
Corresponds with Chapter 7
6 Chapter Functions.
Chapter 9 Objects and Classes
Dr. Bhargavi Dept of CS CHRIST
Constructors and Destructors
Classes and Objects.
Submitted By : Veenu Saini Lecturer (IT)
Classes C++ representation of an object
ENERGY 211 / CME 211 Lecture 8 October 8, 2008.
Classes and Objects Systems Programming.
Presentation transcript:

2 Chapter Classes & Objects

Structures in C Example

Disadvantages of structure Standard C does not treat structure as an built- in data type Suppose we have declared C1,C2,C3 as object of above class at that time we can not perform operation like C3=C1+C2 Structure do not permit data hiding. All structure members are public members.

Structures in C++ C++ support all the features as defined in C, but C++ has some expanded capabilities such as data hiding, Inheritance. In C++ a structure can have both function and variable as member(declaration) It also declare some members as private so that they cannot accessed directly by external function. C++ incorporates all these extension in another user defined type known as Class.

What is Class in C++ A class is a way to bind data and its associated function together. It allows the data and function to be hidden, if necessary. When defining class we are creating new abstract data type that can be treated as other built-in data type

Class specification has two parts: Class declaration Class function definition The class declaration describes the type and scope of its members, The class function definition describe how the class function are implemented

Body of a class is enclosed within braces and terminated by semicolon. The keyword class specifies, that what follows is an abstract data of type class_name Body of a class is enclosed within braces and terminated by semicolon. Class body contains declaration of variables and functions These variabes are collectively called as class members The keyword PUBLIC and PRIVATE are known as visibility labels If we don’t specify this label by default they are PRIVATE. 8

Example

Creating Object Syntax Class_name object_name; Example: item x; OR item x,y,z //multiple object We can also create object after class declaration as we do for structures(i.e declaring after semicolon)

Defining member function Member function can be defined in two places” Outside the class definition Inside the class definition The function should perform the same task irrespective of the place of definition.

Outside definition: An important difference between a member function and normal function is that a member function incorporates a membership ‘identity label’ in the header i.e class_name:: This ‘label’ tells the compiler which class the function belongs to. Syntax

Example

The member function have special characteristics that are often used in the program development. Characteristics are: Several different classes can use the same function name. the membership label will resolve their scope. Member function can access private data of the class The member function can call another member function directly, without using the dot operator

Inside class definition: Another method of defining member function is to replace the function declaration by the actual function definition. When function is defined inside class is called as inline function Normally small functions are defined inside a class.

Example

Making outside function inline: when we define member function outside the class definition and still make it inline by just using the qualifier inline in the header line of function definition

Memory allocation for object The memory space for object is allocated when they are declared and not when the class is specified. This is only partly true the member funaction are created and placed in the memory space olny once when they are defined as a part of class specification Since all the objects belonging to that class use the same member function,no separate space is allocated for member functions when the objects are created

Array of Object Array of a Variables that are of type class are called array of object. Syntax: classname objectname[size]; Example: employee e1[3];

Mememory allocation for array of a object Name Age EMP[0] EMP[1] EMP[2]

Object as function argument Object can be used as function arguement and this is done by two ways. 1. A copy of teh entire object is passed to the function 2.Only the address of the object is transferred to the function

1. A copy of teh entire object is passed to the function The first method is called pass-by-value A copy of a object is passed to the function any changes made to the object inside the function do not affect the object used to call the function

2.Only the address of the object is transferred to the function The second method is called pass-by- reference. When an address of the object is passed, the called function directly work on actual object used in the call. Any changes made to the object inside the function will reflect in the actual object.

Static data members Following are the characteristics of static member It is initialized to zero when the first object of its class is created. No other initialization is permitted Only one copy of that member is crated for the entire class and is shared by all the object of that class, no matter how many objects are created

It is visible only within class, but its lifetime is the entire program. Static variables are used to maintain values common to the entire class. Eg. This can be used as a counter that records the occcurences of all the objects Syntax: static datatype variblename; Eg: static int a;

Static Member function Like a static member variable we can also have static member function. A member function declared with static keyword is called as static member function. static member function has following characteristics: 1.Static function can have access to only other static members/functions declared in same class 2.Static member function can be called using class name(instead of object) as Class_name:: function_name;

Friend Function We have seen that private members can't be accessed from outside class. Non member function cannot have access to the private data of a class. There would be situation where we would like two classes to share particular function. Eg consider two classes manager and scientist. We like to use a function income_tax() to operate on object of both d classes.

C++ allows the common function to be made friendly with the both the classes to have access to the private data of these classes To make an outside function “friendly” to a class, we have to simply declare this function as a friend function of that class. Syntax: Friend returntype function_name(arguements); Eg: Friend void add();

The function that is declared with the keyword friend is known as friend function This function can be defined else where in the program. The function definition does not use either keyword friend or the scope operator:: A function can be declared as a friend in any number of classes. A friend function although not a member function, has full access right to the private members of the class

A friend function has certain special characteristics: It is not in the scope of class to which it has been declared as friend Since it is not in the scope of class, it cannot be us called using object of that class It can be invoked like a normal function without the help of any object. Unlike member function it can’t access member names directly and has to use an object name and dot membership operator with each member name

It can be declared either in the public or private part of a class without affecting its meaning