Classes Introduction CSCI 240

Slides:



Advertisements
Similar presentations
C++ Classes & Data Abstraction
Advertisements

Lecture 3 1. Structures 2. Abstract Data Types. STRUCTURES WHY ARE STRUCTURES NEEDED? If the predefined types are not adequate to model the object, create.
Dale Roberts Object Oriented Programming using Java - Class Constructors Dale Roberts, Lecturer Computer Science, IUPUI Department.
Dale Roberts Object Oriented Programming using Java - Packages Dale Roberts, Lecturer Computer Science, IUPUI Department.
Dale Roberts Introduction to Java - Access Specifiers Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
Dale Roberts Object Oriented Programming using Java - Enumerations Dale Roberts, Lecturer Computer Science, IUPUI Department.
Dale Roberts 1 Classes Constructors & Destructors Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Dale Roberts Object Oriented Programming using Java - OOD to OOP: ATM Case Study Dale Roberts, Lecturer Computer Science, IUPUI
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.
Object Oriented Programming (OOP) Lecture No. 10.
CSC241 Object-Oriented Programming (OOP) Lecture No. 4.
CS 210 DATA STRUCTURES AND ALGORITHIMS Fall 2006.
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.
Dale Roberts Object Oriented Programming using Java - Final and Static Keywords Dale Roberts, Lecturer Computer Science, IUPUI
1 CSC241: Object Oriented Programming Lecture No 02.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
1 Data Structure & Algorithm Pointer & Class. 2 Pointer can be used to store the address of other variables with types of int, char, float, and double.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Information Representation: Negative Integer Representation.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Introduction to Object Oriented Programming (OOP) Object Oriented programming is method of programming.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Elementary Data Structures Array Lists Array Lists Dale.
Introduction to Computers and Programming Class 24 Structures (structs) Professor Avi Rosenfeld.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
Dale Roberts Introduction to Java - Input, Program Control and Instantiation Dale Roberts, Lecturer Computer Science, IUPUI
Dale Roberts Object Oriented Programming using Java - Getters and Setters Dale Roberts, Lecturer Computer Science, IUPUI
1 Data Structures CSCI 132, Spring 2014 Lecture 2 Classes and Abstract Data Types Read Ch Read Style Guide (see course webpage)
Dale Roberts 1 Operator Overloading Member Functions Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
Advanced Programming Constants, Declarations, and Definitions Derived Data Types.
Constructors And Destructors. 2 Constructor Special Member Function used for Initialization -- Same Name as the Class Name Special Member Function used.
CSIS 123A Lecture 1 Intro To Classes Glenn Stevenson CSIS 113A MSJC.
Data Structures Lecture 4: Classes in C++ Azhar Maqsood NUST Institute of Information Technology (NIIT)
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Queues Dale Roberts, Lecturer
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
Object Oriented Programming using Java - Composition
Classes C++ representation of an object
GUI Programming using Java - Key Events
Abstract Data Types Polynomials CSCI 240
Object Oriented Programming using Java - Class Instance Variables
Chapter 3: Using Methods, Classes, and Objects
Variable Declarations, Data types, Expressions
Variable Declarations, Data types, Expressions
Variable Declaration, Data types, Expressions
Abstract Data Types Sparse Matrices CSCI 240
Classes Access Specifiers
CS148 Introduction to Programming II
Advanced Programming Basics
1. Structures 2. Abstract Data Types
Negative Integer Representation
Classes Copy Constructors
Department of Computer and Information Science, School of Science, IUPUI CSCI 265 Classes Dale Roberts, Lecturer Computer Science, IUPUI
Pointers Call-by-Reference CSCI 230
Introduction of Programming
Classes Static Members
Object oriented programming (OOP) Lecture No. 7
Analysis of Algorithms Growth Rates
Classes C++ representation of an object
Data Structures and ADTs
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
CS148 Introduction to Programming II
Classes Class Data Members & Initializers
Abstract Data Types Stacks CSCI 240
Classes Member Qualifiers
Structures Declarations CSCI 230
Presentation transcript:

Classes Introduction CSCI 240 Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Classes Introduction Dale Roberts, Lecturer Computer Science, IUPUI E-mail: droberts@cs.iupui.edu

Class An IMPORTANT FACET OF OOD and C++ A Set of Homogeneous Objects An Abstract Data Type (ADT) A Collection of Data Items or Functions or Both An User Defined Type – describes a new data type Class Definition is a Type Declaration -- No Space is Allocated

Structure of a Class Data Members Member Functions Accessing Rights -- public, private and protected Data members are variables of either fundamental data types or user defined data types. Member functions provide the interface to the data members and other procedures and function. Thus, if any other object or function needs to access the data members, it should do so through the member functions of the class. The default member accessibility is private, meaning that only class members can access the data member or member function. A client uses the public interface meaning it accessed public members. In order to assure data stability of the class, it is recommended that only member functions be public, and the data members are accessed only via get and set functions and procedures.

Syntax of a Class class Class_Name{ private: //Default and Optional data_member_specification_1; : data_member_specification_n; public: data_member_specification_n+1; : data_member_specification_n+m; }; //Don't forget the semicolon after the closing brace. Each class declaration starts with the keyword class followed by the name of the class.  The definition of the class falls between the open and closed braces that follow the class name.

An Example of a Class // date.h /* A Simple Class to Represent a Date */ #define size 50 class date{ private: int day, month, year; char *string_date; public: void set_date( int ip_day,int ip_month,int ip_year); void set_string_date(char *ip_date); };

An Example of a Class // date.cpp #include “date.h” /* Member functions of the "date" class */ void date::set_date(int ip_day,int ip_month,int ip_year) {day = ip_day; month = ip_month; year = ip_year;} void date::set_string_date(char *ip_date) {string_date = new char[size]; strcpy(string_date, ip_date);}

An Example of a Class // client.cpp #include “date.h” /* "today" and "yesterday" are instances of "date" */ main(){ date today, yesterday; today.set_date(19, 06, 1995); today.set_string_date("June 19, 1995"); yesterday.set_date(18, 06, 1995); yesterday.set_string_date("June 18, 1995"); } Compile with % g++ client.cpp date.cpp Later, we’ll learn to write a make file.

Class - Example 2 // student.h /* A Simple Class to Represent a Student */ #define size 50 class Student{   private: char *name; char *id; char *email;   public: void setName(char *studentName); void setId(char *studentId); void setEmail(char *studentEmail); };

Class – Example 2 (cont) // student.cpp #include “student.h” /* Member functions of the “Student" class */ void Student::setName(char *studentName) {name = new char[size]; strcpy(name, studentName);}    void Student::setId(char *studentId) {id = new char[size]; strcpy(id, studentId);} void Student::setEmail(char *studentEmail) {email = new char[size]; strcpy(email, studentEmail);}

Class – Example 2 contd…. // client.cpp #include “student.h” /* “csStudent" is an instance of “Student" */ main(){     Student csStudent; // Allocates space     csStudent.setName("Susie Creamchese");     csStudent.setId("123456789");     csStudent.setEmail("screamch@iupui.edu"); }

Acknowledgements These slides were originally prepared by Rajeev Raje, modified by Dale Roberts.