Download presentation
Presentation is loading. Please wait.
Published byMaximilian Craig Modified over 8 years ago
1
February 28, 2005 Introduction to Classes
2
Object Oriented Programming An object is a software bundle of related variables and methods. Software objects are often used to model real-world objects you find in everyday life. Software objects interact and communicate with each other using messages also called methods. A class is a blueprint or prototype that defines the variables and the methods common to all objects of a certain kind
3
Object-Oriented Viewpoint An application is a set of objects interacting by sending messages The functionality of an object is described by its methods, it’s data is stored in private variables An object’s functionality can be invoked by sending a message Everything is an object
4
Classes A simple class #include class Student { string firstName; } ;
5
A good start but there is a problem #include using namespace std; class Student { string firstName; }; int main() { Student s; s.firstName = "Jack"; cout << “My first name is “ << s.firstName << endl; return 0; }
6
By default, member variables are private #include using namespace std; class Student { string firstName; }; int main() { Student s; s.firstName = "Jack"; cout << “My first name is “ << s.firstName << endl; return 0; }
7
We could do this but we shouldn’t #include using namespace std; class Student { public: string firstName; }; int main() { Student s; s.firstName = "Jack"; cout << “My first name is “ << s.firstName << endl; return 0; }
8
This is better #include using namespace std; class Student { private: string firstName; public: void setFirstName(string name) { firstName = name; } }; int main() { Studennt s; s.setFirstName ("Jack"); cout << "My first name is " << s.getFirstName () << "." << endl; return 0; }
9
This is better still #include using namespace std; class Student { private: string firstName; public: string getFirstName() { return firstName; } void setFirstName(string name) { firstName = name; } }; int main() { Student s; s.setFirstName ("Jack"); cout << "My first name is " << s.getFirstName () << "." << endl; return 0; }
10
This is even better #include using namespace std; class Student { private: string firstName; public: string getFirstName(); void setFirstName(string name) }; string Student::getFirstName() { return firstName; } Void Student::setFirstName(string name) { firstName=name; } int main() { Student s; s.setFirstName ("Jack"); cout << "My first name is " << s.getFirstName () << "." << endl; return 0; }
11
Using Header files To better organize our classes and programs we can put declarations in a header file and implementations in a source file Let’s do this for the Student class To avoid recursively including header file more than once, use this preprocessor directive #ifndef STUDENT_H #define STUDENT_H #endif
12
Student.h #ifndef STUDENT_H #define STUDENT_H #include using namespace std; class Student { private: string firstName; public: string getFirstName(); void setFirstName(string name); }; #endif
13
Student.cpp #include “Student.h" #include string Student::getFirstName () { return firstName; } void Student::setFirstName (string name) { firstName = name; }
14
main.cpp #include #include “Student.h" using namespace std; int main() { Student s; s.setFirstName("Jack"); cout << "My first name is " << s.getFirstName() << "." << endl; return 0; }
15
Inline Member Functions It is possible to place a member function’s body in the class declaration instead of its prototype. This is often done when a member function has a small body The result of this is that the compiler will replace calls to the function with the actual body of the function. This eliminates the overhead associated with calling a function. This makes the code bigger but can improve performance.
16
Student.h with inline member function #ifndef STUDENT_H #define STUDENT_H #include using namespace std; class Student { private: string firstName; public: string getFirstName() { return firstName; } void setFirstName(string name); }; #endif
17
Constructors It would be nice to have a member function that: Initializes a class in anyway deemed necessary by the class writer. Is called automatically whenever an instance of the class is created. There is such a member function and it is called a Constructor.
18
Constructors Constructors always have the same name as the class. A constructor has no return type, not even void. This is because constructors cannot be explicitly called and cannot return values.
19
Student Class Header with Constructor #ifndef STUDENT_H #define STUDENT_H #include using namespace std; class Student { private: string firstName; public: Student(); //Constructor string getFirstName() { return firstName; } void setFirstName(string name); }; #endif
20
Creating an assigning a random ID to each student in a class Let’s do this at the time we create a Student object. First add a private member variable of time int called studentID Inside of Student.h Student(); Inside of Student.cpp Student() { srand(time(NULL)) make sure to include studentTime = rand(); }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.