Class Student class Student { private: string id; string firstName, lastName; float gpa; public: void Read() void Write() string getGPA() void setGPA(

Slides:



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

The Line Class Suppose you are involved in the development of a large mathematical application, and this application needs an object to represent a Line.
Class Scope class Student { private: string id; string firstName, lastName; float gpa; public: void Read() { cin >> id >> firstName >> lastName >> gpa;
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
1 Chapter 11 Introducing the Class Pages ( )
Introduction to Programming Lecture 39. Copy Constructor.
Student Data Score First Name Last Name ID GPA DOB Phone... How to store student data in our programs? 1.
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
CSE 2501 Review Declaring a variable allocates space for the type of datum it is to store int x; // allocates space for an int int *px; // allocates space.
1. 2 Introduction to Methods  Method Calls  Parameters  Return Type  Method Overloading  Accessor & Mutator Methods  Student Class: Revisited.
C++ data types. Structs vs. Classes C++ Classes.
Variables, Data Types, & Arithmetic Expressions CSC 1401: Introduction to Programming with Java Lecture 3 Wanda M. Kunkle.
Java Classes Introduction and Chapter 1 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
1 Class Constructors a class constructor is a member function whose purpose is to initialize the private data members of a class object the name of a constructor.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
SE-1010 Dr. Mark L. Hornick 1 Defining Your Own Classes Part 3.
CSC 212 Object-Oriented Programming and Java Part 1.
Programming Languages and Paradigms Programming C++ Classes.
Introduction to Java Classes and Objects. What is a class A class is description of a structure that contains both data and methods – Describes a set.
Week 9 - Monday.  What did we talk about last time?  Time  GDB.
 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.
Class Inheritance Dr. Leon Jololian. Dr.Jololian2 class Person { private: string name; int age; public: Person(string na, int ag); Person(string na);
RECITATION 4. Classes public class Student { } Data Members public class Student { private String name; public String id; }
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
Arrays and Strings. Why? Consider a class of 30 students, each has a score for hw1  Do we want to have 30 variables with different names?  Would it.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
Chapter 7 A Data Types – Structures Structures Structure: C++ construct that allows multiple variables to be grouped together Structure Declaration.
Structure TDate struct TDate { int year, month, day; }; // Define a new data type.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
Class Constructors class Student { private: string id, firstName, lastName; float gpa; public: Student() Student(string sID) Student(string first, string.
Chapter 8: Designing Classes Accessors, Mutators, and Immutable Classes(8.3) Side Effects(8.4) Parameter Passing (Advanced Topic 8.1) Preconditions and.
CS 1430: Programming in C++.
CS 1430: Programming in C++ 1. Class StudentList class StudentList { private: int numStudents; Student students[MAX_SIZE]; int find(const Student& s)
Multiple Items in one Data Object Arrays are a way to store more than one piece of data in a data object, provided that all the data is of the same type.
Class Method Read class Student { private: string id; string firstName, lastName; float gpa; public: // Will the method change any data members? // Yes!
CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis & Matt Bauer.
Classes - Intermediate
Exam 2 EXAM 2 Thursday!!! 25% of Final Grade Know: loops, switch/case Files Input failure (e.g. scan.hasNextInt())
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7. 12: Structures Starting Out with C++ Early Objects Eighth.
CS 1430: Programming in C++ 1. File Input in VS Project Properties Debugging Command Arguments quiz8-1.out We want to know how to do it ourselves, right?
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 4th Edition.
CLASSES IN JAVA Primitive Types Example of a Class Allocating Objects of a Class Protecting Class data Constructors Static data and Static Methods.
Nested Structures struct TDate { int year, month, day; }; struct StudentType { string id, firstName, lastName; float gpa; TDate DOB; }; struct SectionType.
Defining Your Own Classes II
Classes (Part 1) Lecture 3
Yanal Alahmad Java Workshop Yanal Alahmad
Intro To Classes Review
COMPUTER 2430 Object Oriented Programming and Data Structures I
Structs versus Classes
Anatomy of a class Part I
group work #hifiTeam
CS 1430: Programming in C++.
Student Data Score First Name Last Name ID GPA DOB Phone ...
Data Types – Structures
CS 1430: Programming in C++.
Pass by Reference, const, readonly, struct
CS 1430: Programming in C++.
CSC 113 Tutorial QUIZ I.
CS 1430: Programming in C++.
Enumeration Data Type enum Day {SUN, MON, TUE, WED, THU, FRI, SAT};
Classes & Objects: Examples
CS 1430: Programming in C++.
COMPUTER 2430 Object Oriented Programming and Data Structures I
CPS120: Introduction to Computer Science
JAVA CLASSES.
NAME 436.
Structure (i.e. struct) An structure creates a user defined data type
C++ data types.
Anatomy of a class Part I
Week 9 - Monday CS222.
Presentation transcript:

Class Student class Student { private: string id; string firstName, lastName; float gpa; public: void Read() void Write() string getGPA() void setGPA( float value ) void updateGPA( float amount )... }; // Student is a data type // Data and functions together 1

Class and Object // Declare variable and reserve memory space int numStudents; // Declare class variable // memory space for data fields // methods Student s1; // Input data into object s1 s1.Read(); // No values for data members s1.Write(); 2

Initial Values // Declare variable with initial value int numStudents = 0; // Can we declare class variable with initial value? Student s1; Use Class Constructors! 3

Class Default Constructor class Student { private: string id; string firstName, lastName; float gpa; public: Student() { id = “12345”; firstName = “first”; lastName = “last”; gpa = 2.5; } }; // Syntax for constructor // Default constructor has no parameters 4

Default Class Constructor // Default constructor is called Student s1; // The default constructor has set the // values for data members s1.Write(); // This will change the data fields s1.Read(); // Output data of object s1 s1.Write(); Constructor is called implicitly when class variables are declared. 5

Constructor with Parameters class Student { private: string id; string firstName, lastName; float gpa; public: Student(string sID, string first, string last, float g) { id = sID; firstName = first; lastName = last; gpa = g; } }; 6

Constructor with Parameters // Class constructor is called Student s1(“12345”, “John”, “Smith”, 2.8); // The constructor has set the // values for data members s1.Write(); // This will change gpa s1.UpdateGPA(0.1); // Output data of object s1 s1.Write(); 7

Class Constructors with Different Parameters class Student { private:... public: Student(string sID) { id = sID; // could set other default values } Student(string first, string last) { firstName = first; lastName = last; gpa = 2.5; // can still set gpa } }; 8

Class Constructors // Student(string sID) is called Student s1(“56789”); // Student(string first, string last) is called Student s2(“Qi”, “Yang”); // Student(String sID, string first, string last, // float g) is called Student s3(“13579”, “Joe”, “Clifton”, 3.9); // Student() is called Student s4(); 9

Different Constructors Must Have Different Parameters class Student { private:... public: Student(string last, string first) { firstName = first; lastName = last; } Student(string first, string last) { firstName = first; lastName = last; } }; Will this work? 10

Different Constructors Must Have Different Parameters // Which one will be called? // Student(string first, string last) // Student(string last, string first) Student s(“Qi”, “Yang”); 11

Different Parameters: Different Type class Student { private:... public: Student(string last, string first) { firstName = first; lastName = last; } Student(string sID, float g) { id = sID; gpa = g; } }; 12

Different Parameters: Different Type // Student(string first, string last) is called Student s(“Qi”, “Yang”); // Student(string sID, float g) is called Student s(“12345”, 3.3); 13

Class Constructors class Student { private:... public: Student() Student(string sID) Student(string first, string last) Student(string sID, float g) Student(string sID, string first, string last, float g) }; 14

Class Constructors // Student(string sID) is called Student s1(“56789”); // Student(string first, string last) is called Student s2(“Qi”, “Yang”); // Student(String sID, string first, string last, // float g) is called Student s3(“13579”, “Joe”, “Clifton”, 3.9); // Student() is called Student s4(); // Student(string sID, float g) is called Student s(“12345”, 3.3); 15

No Default Constructor class Student { private:... public: // Student() Student(string sID) Student(string first, string last) Student(string sID, float g) Student(string sID, string first, string last, float g) }; 16

No Default Constructor // Student(string sID) is called Student s1(“56789”); // Student(string first, string last) is called Student s2(“Qi”, “Yang”); // Student(String sID, string first, string last, // float g) is called Student s3(“13579”, “Joe”, “Clifton”, 3.9); // Student(string sID, float g) is called Student s(“12345”, 3.3); // No default constructor if a constructor is defined. Student s4(); 17

No Constructors class Student { private:... public: /* Student() Student(string sID) Student(string first, string last) Student(string sID, float g) Student(string sID, string first, string last, float g) */ }; 18

Class Constructors // Invalid Student s1(“56789”); // Invalid Student s2(“Qi”, “Yang”); // Invalid Student s3(“13579”, “Joe”, “Clifton”, 3.9); // Invalid Student s(“12345”, 3.3); // Default constructor is good! Student s4(); // Will not work! s4.Write(); s4.UpdateGPA( 0.1 ); 19

Copy Constructor class Student { private: string id, firstName, lastName; float gpa; public: Student(string sID, string first, string last, float g) // Copy constructor: // gets initial values from object s Student( Student s) { id = s.id; firstName = s.firstName; lastName = s.lastName; gpa = s.gpa; } }; 20

Class Constructors // Student(String sID, string first, string last, // float g) is called Student s1(“13579”, “Joe”, “Clifton”, 3.9); int x, y = 10; // Copy constructor is called Student s2(s1); // Similar to the copy constructor x = y; s2.Write(); s2.UpdateGPA( 0.1 ); s2.Write(); 21

Schedule Quiz7-2: Due Wednesday Program 5 Pairs by Wednesday 22

23 Test 1 Test 2 Test 3Final

24 Good Luck! Come to classes/labs Do the work by yourself Get Help