CS 1430: Programming in C++.

Slides:



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

Operator overloading redefine the operations of operators
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Class Scope class Student { private: string id; string firstName, lastName; float gpa; public: void Read() { cin >> id >> firstName >> lastName >> gpa;
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.
Enumeration Data Type enum Day {SUN, MON, TUE, WED, THU, FRI, SAT}; Day today; today = WED; if (today == FRI) cout
1. 2 Introduction to Methods  Method Calls  Parameters  Return Type  Method Overloading  Accessor & Mutator Methods  Student Class: Revisited.
CS 117 Spring 2002 Classes Hanly: Chapter 6 Freidman-Koffman: Chapter 10, intro in Chapter 3.7.
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.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Definition Class In C++, the class is the construct primarily used to create objects.
Operator Overloading CS 308 – Data Structures What is operator overloading? Changing the definition of an operator so it can be applied on the objects.
3.1 Documentation & Java Language Elements Purpose of documentation Assist the programmer with developing the program Assist other programers who.
Elements of a C++ program 1. Review Algorithms describe how to solve a problem Structured English (pseudo-code) Programs form that can be translated into.
Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
Classes CS 21a: Introduction to Computing I First Semester,
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.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
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; }
Structured Data Types struct class Structured Data Types array – homogeneous container collections of only one type struct – heterogeneous data type.
Structure TDate struct TDate { int year, month, day; }; // Define a new data type.
CSC 212 Object-Oriented Programming and Java Part 2.
Class Constructors class Student { private: string id, firstName, lastName; float gpa; public: Student() Student(string sID) Student(string first, string.
Class Student class Student { private: string id; string firstName, lastName; float gpa; public: void Read() void Write() string getGPA() void setGPA(
CS 1430: Programming in C++ 1. Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
1 CS 1430: Programming in C++. Quiz Functions Function Prototype float sqrt(float x); Function name, type, parameter and parameter type Function.
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!
Struct s (7.4) Used as data aggregates for an entity can be different types of data e.g. for student id, name, GPA, address,... Similar to classes, but.
CS 1430: Programming in C++ 1. Test 2 Friday Functions Arrays For Loops Understand Concepts and Rules Memorize Concepts and Rules Apply Concepts and Rules.
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?
Arrays float Scores[9]; ? index: element // one dimensional array 1.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
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
Constructors and Destructors
Classes (Part 1) Lecture 3
COMPUTER 2430 Object Oriented Programming and Data Structures I
Yanal Alahmad Java Workshop Yanal Alahmad
COMPUTER 2430 Object Oriented Programming and Data Structures I
group work #hifiTeam
CS 1430: Programming in C++.
CS 1430: Programming in C++.
Student Data Score First Name Last Name ID GPA DOB Phone ...
CS 1430: Programming in C++.
CS 1430: Programming in C++ Turn in your Quiz1-2 No time to cover HiC.
CS 1430: Programming in C++.
Defining Your Own Classes
Enumeration Data Type enum Day {SUN, MON, TUE, WED, THU, FRI, SAT};
CS 1430: Programming in C++ No time to cover HiC.
Strings A collection of characters taken as a set:
CS 1430: Programming in C++.
CS148 Introduction to Programming II
Constructors and Destructors
CS 1430: Programming in C++.
COMPUTER 2430 Object Oriented Programming and Data Structures I
CPS120: Introduction to Computer Science
Classes CS 21a: Introduction to Computing I
Class StudentList class StudentList { private: int numStudents;
Structure (i.e. struct) An structure creates a user defined data type
C++ data types.
Classes and Objects Systems Programming.
Week 9 - Monday CS222.
Introduction to Classes and Objects
Presentation transcript:

CS 1430: Programming in C++

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

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();

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

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

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 Default Constructor is called implicitly when class variables are declared.

Class Default Constructor class Student { private: string id; string firstName, lastName; float gpa; public: Student() id = “”; firstName = “”; lastName = “”; gpa = 0.0; } }; // Empty strings and zero: special values. // Not garbage!

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; } };

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

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 = 0.0; // can set gpa with a default value };

Class Constructors // Student(string sID) is called // 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();

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

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”);

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; };

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);

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) };

Class Constructors // Student(string sID) is called // 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);

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) };

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(); // Invalid!

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) */ };

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

// -------------------------------------------------- // Each class must have a comment block // to describe it. class Student { private: string id; string firstName, lastName; float gpa; public: // constructors Student() . . . Student(string sID, string first, string last) // methods void Read() void Write() float GetGPA() };

class Student { private: string id, firstName, lastName; float gpa; // Why do we need it? // Can we do this? // What is this? Student (string sID) id = sID; } public: // constructors Student() . . . Student(string sID, string first, string last) // methods void WhatMethod(. . .) };

class Student { private: string id, firstName, lastName; float gpa; // private constructor Student (string sID) . . . // private method // what is this? int WhatToDo() public: // constructors Student() Student(string sID, string first, string last) // methods void WhatMethod(. . .) };

class Student { private: string id, firstName, lastName; float gpa; Student (string sID) int WhatToDo() public: Student() Student(string sID, string first, string last) // methods void WhatMethod() void Read() void Write() string GetFirst() float getGPA() void setGPA( float value ) void updateGPA( float amount ) };

class Student { private: . . . int WhatToDo() public: void WhatMethod() }; int main() Student s1, s2; s1.Read(); s1.Write(); s1.SetGPA(3.1); s1.WhatMethod(); // valid? s1.WhatToDo(); // valid? return 0; }

class Student { . . . }; int main() Student s1, s2; s1.Read(); s1.Write(); Student s3(“Qi”, “Yang”, “1418”); s3.SetGPA(3.1); s2 = s3; // valid? s2.Write(); if (s1 > s2) // valid? cout << “Student ” << s1.GetFirst() << “ is better than ” << s2.GetFirst() << ‘.’; return 0; }

class Student { . . . }; int main() Student s1, s2; s1.Read(); s1.Write(); Student s3(“Qi”, “Yang”, “1418”); s3.SetGPA(3.1); s2 = s3; // Good! Copy all fields. s2.Write(); if (s1 > s2) // Invalid! cout << “Student ” << s1.GetFirst() << “ is better than ” << s2.GetFirst() << ‘.’; return 0; }

class Student { . . . }; int main() Student s1, s2; Student s3(“Qi”, “Yang”, “1418”); s3.SetGPA(3.1); s2 = s3; // Good! Copy all fields. if (s1.GetGPA() > s2.GetGPA()) // Valid! cout << “Student ” << s1.GetFirst() << “ is better than ” << s2.GetFirst() << ‘.’; cin >> s1; // Valid? cout << s1; // Valid? return 0; }

Schedule Quiz7-2: Due Monday Program 4 Lab8 Test 2

Prog4 Five required functions You can make additional functions Follow the Programming Rules