CP1_13_Classes_2_1112

CP1_13_Classes_2_1112 - Classes (cont.) Classes (cont.)...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Classes (cont.) Classes (cont.) Defining classes Defining classes In the first lecture on classes we used the pre-defined library class string to introduce features of classes and how to use em to create and manipulate objects. them to create and manipulate objects. We now look at the details of defining and using your own lasses emember classes encapsulate variables and classes . Remember, classes encapsulate variables and functions related to the concept represented by the class. The syntax for defining a class is: class Class_name { access_specifier: As before, member variable or member function encapsulated in member; member; … the class access_specifier: member; member; 2 CSIS1117B Computer Programming 1 2011-2012 … }; Example Example class Student { ublic: public: void initialize(string name, string u_no); double getGPA(); Member function eclaration … private: string student_name; tring number declaration string u_number; double gpa; … }; }; Many classes may have a member function named initialize . When we write a definition for the function, we must tell the compiler which class the definition belongs to. We do this using the scope resolution operator :: oid Student::initialize(string name string no void Student::initialize(string name, string u_no) { student_name = name; number no Member function definition 3 CSIS1117B Computer Programming 1 2011-2012 u_number u_no; gpa = 0.0; } class Student { public: void initialize(string name, string u_no); double getGPA(); … private: string student_name; string u_number; double gpa; … }; As we saw previously, we invoke member functions using the dot operator with an object of type Student. ouble Student:: etGPA ) { double Student::getGPA() { return gpa; } nt ain ) { int main() { Student student1; student1.initialize("Bjarne Stroustrup", "2011123456"); out < tudent1.getName ) <<tudent1....
View Full Document

Page1 / 11

CP1_13_Classes_2_1112 - Classes (cont.) Classes (cont.)...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online