Course Title Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 07 classes 1 BY ADEEL ANJUM ( MSc - cs, CCNA, WEB DEVELOPER )

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.
Spring Semester 2013 Lecture 5
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions.
Structure.
Functions in C++. Functions  Groups a number of program statements into a unit & gives it a name.  Is a complete and independent program.  Divides.
What have we learned so far… Preprocessor directives Introduction to C++ Variable Declaration Display Messages on Screen Get Information from User Performed.
A program example is given below to input date and display on the screen by write a class ‘ date ‘ # include Class date { Private: Int y, m, d ; Publice.
Classes and Objects Presented by: Gunjan Chhabra.
True or false A variable of type char can hold the value 301. ( F )
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
C++ fundamentals.
By: Mr. Baha Hanene Chapter 3. Learning Outcomes We will cover the learning outcome 02 in this chapter i.e. Use basic data-types and input / output in.
CSCI 1730 January 17 th, 2012 © by Pearson Education, Inc. All Rights Reserved.
Abstract Data Types Using Classes Lecture-5. Abstract Data Types Using Classes Representing abstract data types using C++ We need the following C++ keywords.
Object Oriented Programming (OOPs) Class Object Data Hiding Abstraction Encapsulation Polymorphism Inheritance Difference between Procedural and OOPs programming.
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Structures  2 nd aggregate data type: struct  Recall:
Course Title Object Oriented Programming with C++ Course instructor ADEEL ANJUM Chapter No: 05 ARRAY 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER) 1.
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
Week 1 Algorithmization and Programming Languages.
Course Title: Introduction to C++ Course Instructor: ADEEL ANJUM Chapter No: 01 1 BY ADEEL ANJUM (MCS, CCNA,WEB DEVELOPER)
Learners Support Publications Classes and Objects.
Structures in C++ UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) 1 ADNAN BABAR MT14028 CR.
Course Title: Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
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
Welcome to Classes and Objects Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्जेण्डर )TGT(CS) KV JHAGRAKHAND.
1 CSC241: Object Oriented Programming Lecture No 02.
Structures and Classes Version 1.0. Topics Structures Classes Writing Structures & Classes Member Functions Class Diagrams.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
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.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Introduction to Object Oriented Programming (OOP) Object Oriented programming is method of programming.
Course Title Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 06 FUNCTIONS 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER) 1.
Classes, Interfaces and Packages
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.
Course Title Object Oriented Programming with C++ Course instructor ADEEL ANJUM Chapter No: 04 loops 1 BY ADEEL ANJUM ( MSc - cs, CCNA, WEB DEVELOPER )
Course Title Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
MY COLLEGE: BITM SANTINIKETAN Object oriented programming lets check through C++
Functions Chapter 5. Function A set of instructions that are designed to perform specific task. A complete and independent program. It is executed by.
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
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.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
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.
Lecture 3 (UNIT -1) SUNIL KUMAR CIT-UPES.
Structures and Classes
2 Chapter Classes & Objects.
Institute of Business & Technology (BIZTEK)
Objectives Identify the built-in data types in C++
About the Presentations
Classes & Objects.
Concepts and Basics of C++ Programming
Chapter 5 Classes.
This technique is Called “Divide and Conquer”.
Concepts and Basics of C++ Programming
Lecture 4-7 Classes and Objects
Chapter 3 Introduction to Classes, Objects Methods and Strings
Learning Objectives Classes Constructors Principles of OOP
Classes and Objects.
Object Oriented Programming in java
Presentation transcript:

Course Title Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 07 classes 1 BY ADEEL ANJUM ( MSc - cs, CCNA, WEB DEVELOPER )

CLASSES The most important feature of c ++ programming language is that it supports object oriented programming. In oop the program is divided into objects. oop is an easy and flexible approach of designing and organizing the program. In this chapter the concepts of classes and objects are discussed. 2

CLASSES A class is a collection of data and functions. The data items and functions are defined within the class. The function are written to work upon the data items and each function has a unique relationship with the data items of the class. 3

Conti… class are defined to create user defined data types. These are similar to built in data types available in all programming languages. When a class is defined, it does not occupy any space in the computer memory. It only defines the data items and the member function that can be used to work upon its data items. 4

DEFINING A CLASS A class is defined in a similar way as a structure is defined. The keyword “class” is used to define the class. The general syntax to define a class is : class class-name { body of class}; class: it is the keyword that is used to define a class.

Conti…… class-name :it represent the name of class. The rules to give the name of a class and a variable are same. Any word that can be a variable name in a c ++. 6

conti… body of class: The body of a class consists of the data items and the functions. These are called members of the class. These are written between braces. semicolon: The body of a class ends with semicolon. 7

MEMBER S OF A CLASS A class contains data items and functions. These are called members of the class. The data items are called data members and the functions are called member function. 8

DATA MEMBERS The data items of a class are called data member of the class, for example : A class that has four integer type and two float type data item is declared as 9

conti.. class abc { int w, x, y, z; float a, b ; } ; in the above class a, b, w, w, y and z are data members of the class “abc”. 10

MEMBER FUNCTIONS the function of a class that are defined to work on its data members are called member functions of the class. the member functions may be defined with a class or out side it. for example: 11

conti… class xyz { private : int a, b, c ; Public : void get(void) { cout <<“ Enter value of a, b and c “; cin>> a>>b>>c; void pout(void) { cout<<“a=“<<a<<endl; cout<<“b=“<<b<<endl; cout<<“c=“<<c<<endl; } }; 12

In the above class, there are three data member and two member functions. The member function are “get” and “pout”. The “get” function is used to input value into data member a, b and c. The “pout “ function is used to print value of the data members on the computer screen. 13

MEMBER ACCESS SPECIFIERS The commands that determines whether a member of a class can be accessed from out side the class or not are called member access specifies. Normally two types of member access specifies are used. These are : 1.private: 2.public: 14

PRIVATE SPECIFIER The member of a class that can be accessed only from within the class are called private members. They can not be accessed from out side the class. normally all data members are declared as private. The members that are made private can only be accessed from within the class. 15

Conti… A class member that come after the specifier private : and up to the next member access specifier are declared as a private and or accessible only from inside the class. The default access mode is private. Thus if no access specifier is given, the members are treated as a private. 16

PUBLIC SPECIFIER making a data to be access from within the class is called data hiding. i.e. the data declared as private is hidden from outside the class. public members of a class can be accessed both from inside and from out side the class. 17

CONTI.. Normally member functions are declared as public. Data members can also be declared as a public. The following example explain the use of member access specifier. in c

class cdate { private : int y,m,d; public : void gdata() { cout<<“year ?”; cin>>y; cout<<“month”; cin>>m; cout<<“days”; cin>>d; } void pdata() { cout<<d<<“/”<<m<<“/”<<y;} }; 19

Conti……. in the above example, the class has been defined its name is “cdate”. It has three data members y,m,d of integer type and has been declared as a private. These data members can only be accessed from inside this class. 20

Conti…… The class also has two members functions “gdate” and “pdate”. The “gdate” function get date and “pdate” function print the date in date format. (dd / mm / yyyy) on the computer screen. Both the function member are declared as a public. 21

OBJECTS A data type is used to declare a variable. A variable of a data type is also known as the instance or case of that data type. Each variable has a unique name but each variable follows the rules of its data type. When a variable of a data type is declared some space is reserved for it in the memory. 22

conti…. A class is also like a data type. It is therefore used to declare variable or instance. The variable or instance of a class are called objects. A class may contain several data items and function. Thus the objects of the class consists of both the data members and the member functions of the class. 23

conti… The combining of both the data and the function into one unit is called data encapsulation. An object represent data members of a class in the memory. Each object of a class has a unit name. The name of an object differentiate it from other objects of the same class. 24

Conti….. The values of data members of different objects may be different or same. The values of data members in an objects are known as the state of the object. The functions in an object are called the members functions. They are also known as the method. The member functions are used to process and access data of the object. 25

conti… For example the data from an object is accessed and processed through the member functions of the objects. It can not directly read the data in the object. The data is hidden for other objects of the program this is called data hiding. the data hiding and data encapsulation are the main features of object oriented programming. 26

DECLARING OBJECTS OF A CLASS The objects of a class are declared in the similar way as the variables of any data type are declared. when a class is defined,no space is reserved for it in memory. It only provide information how its object will look like. When an object of a class is declared, a memory space is reserved for that object. 27

The syntax to create objects of class type is: class_name object_name separated by commas; For example, to define an object “b” of class “oop” the statement is written as: oop b; in the above statement one object “b” is declared of class “oop”. It is the declaration of an object that actually creates an object in the memory. 28

CALLING MEMBER FUNCTION The member function of a class is called in a similar way as member data is called. The member function is called through an object of the class. The dot operator is used. The dot operator connects the object name and the member function. 29

Syntax to call a member function object name.member function; For example,if “add” is the name of the object and “pdate” is the member function then the member function is called as: add.pdate(); The dot operator is called the class member access operator. 30

#include Class edate { private: int y, m, d; public: void gdate(void); { cout<<“Enter year?”; cin>>y; cout<<“Month”; cin>>m; cout<<“Enter Day”; cin>>d; } void pdate(void) { cout<<“Date is:”<<endl; cout<<d<<“/”<<m<<“/”<<y; } }; main(){ edate ani; clrscr(); ani.gdate(); ani.pdate(); } 31

Conti…… out put of program Enter year 1965 Enter month 1 Enter day 27 date is : 27/1/

In the above program the class “edate” has three data member y, m, d of int type and two member function “gdate” and “pdate”. The definition of both functions are inside the class “edate”. The function “gdate” inputs the values of year,month and day. The function “pdate” prints the date in the date formate. 33

void main() { sum s; int x, y; cout<<“Enter first no”; cin>>x; cout<<“Enter second no”; cin>>y; s.get(x, y); s.display(); getch(); } #include class sum { private: int n,m; public: void get (int a, int b) { n=a; m=b; } void display (void) { cout<<“Sum=”<<n+m; } }; 34

void display(void) { clrscr(); cout<<“Name of employee:”<<name<<endl; cout<<“Basic pay:”<<bpay<<endl; cout<<“House rent :”<<h_rent<<endl; cout<<“Medical allowance”<<ma<<endl; cout<<“Net pay :”<<gpay<<endl; } }; void main(void) { emp dd; clrscr(); dd. allow(); dd.display(); getch(); } #include class emp { private: char name[15]; float bpay, h_rent,ma, gpay; public: void input (void) { cout<<“Enter name of employee” cin>>name; cout<<“Basic pay”; cin>>bpay; } void allow (void) { h_rent = bpay * 60/100; ma = bpay * 20/100; gpay =bpay + h_rent + ma; } 35

void show() { clrscr(); cout<<“Name of student:”<<name<<endl; cout<<“Marks of oop:”<<oop<<endl; cout<<“Marks of dld:”<<dld<<endl; cout<<“Total marks:”<<tot<<endl; cout<<“Average:”<<avg<<endl; } }; void main () { std abc; clrscr(); abc.getrec(); abc. show(); getch(); } #include class std { private: char name[15]; float oop, dld, tot, avg ; public: void getrec(void) { cout<<“Enter name of student”; cin>>name; cout<<“Enter oop marks”; cin>>oop; cout<<“Enter dld marks”; cin>>dld; tot = oop + dld; avg = tot / 3.0; } 36

DEFINING MEMBER FUNCTION OUTSIDE THE CLASS Member function of a class can also be defined outside the class. In this case, only the prototype of the member function is declared inside the class. The member functions are defined outside the class in the similar way as user defined functions are defined. However,the scope resolution operator (::) is used in the member function declarator to define the function of class outside the class. 37

Syntax type class name:: function name() { body of function } TYPE : represent the data type of value returned by the member function. CLASS NAME: represent the class name to which the member function belongs. 38

Conti… :: double colon represents scope resolution operator that is used to define the body of member function of a specified class. FUNCTION NAME: represents the name of the member function of a class with argument. BODY OF FUNCTION: it is set of statements of the function. 39

void emp :: input (void) { cout<<“Enter name of employee” cin>>name; cout<<“Basic pay”; cin>>bpay; } void emp:: display(void) { clrscr(); cout<<“Name of employee:”<<name<<endl; cout<<“Basic pay:”<<bpay<<endl; cout<<“House rent :”<<h_rent<<endl; cout<<“Medical allowance”<<ma<<endl; cout<<“Net pay :”<<gpay<<endl; } void emp:: allow (void) { h_rent = bpay * 60/100; ma = bpay * 20/100; gpay =bpay + h_rent + ma; } #include class emp { private: char name[15]; float bpay, h_rent,ma, gpay; public: void input(void); void allow(void); void display(void); }; void main(void) { emp dd; clrscr(); dd. allow(); dd.display(); getch(); } 40

End of chapter…..