Download presentation
Presentation is loading. Please wait.
1
CS31 Discussion 1D Winter18: week 6
TA: Behnam Shahbazi Some slides former TAs: Chelsea Ju
2
Struct Motivation Syntax: Declaration & Initialization:
Allow user-defined data type that contains a collection of different data type Syntax: struct TypeName{ member1; member2; }; Declaration & Initialization: TypeName varName = {var1, var2};
3
Struct Motivation Example:
Allow user-defined data type that contains a collection of different data type Example: #include <iostream> using namespace std; struct date{ int day; int month; int year; }; int main () { date birthday = {10, 10, 1990}; date yesterday; yesterday.year = 2015; yesterday.month = 3; yesterday.day = 5; } Don’t forget the semicolon
4
Struct Valid Operation Invalid Operation Assignment Function Argument
Ex: date tomorrow = {7, 3, 2015}; date examDate = tomorrow; Function Argument Call-by-value Call-by-reference Return Value Invalid Operation Arithmetic Comparison I/O
5
Struct Access the member variable in struct
As a normal variable: “.” operator As a pointer variable: “->” operator Ex: #include <iostream> using namespace std; int main () { struct date{ int day; int month; int year; }; date birthday, *pbirthday; birthday = {10, 10, 1990}; pbirthday = &birthday; cout << birthday.day << endl; cout << pbirthday -> month << endl; cout << (*pbirthday).year << endl; }
6
Class Very similar to struct Except…
All the member variables and member functions are public in struct, but private in class by default struct employee { int ID; double salary; string jobtitle; void raise(){ salary *= 1.5; } }; class employee { int ID; double salary; string jobtitle; void raise(){ salary *= 1.5; } };
7
Public vs Private Public: Private: Ex:
Public members can be used directly by objects of other classes Private: Private members that are hidden from other classes It is only accessible in its member function Ex: #include <iostream> int main(){ #include <string> employee emp; using namespace std; emp.ID = 100; emp.jobtitle = "Student"; class employee { private: emp.salary = ; //not allow double salary; public: int ID; emp.setSalary(1000.0); string jobtitle; //use member function to set value void setSalary(double s){ salary = s; } return 0; };
8
Member Function Define outside the class Define inside the class
Syntax Classname::functionName() Define inside the class functionName() class employee { private: double salary; public: int ID; string jobtitle; void setSalary(double s); }; void employee::setSalary(double s){ salary = s; } class employee { private: double salary; public: int ID; string jobtitle; void setSalary(double s){ salary = s; } };
9
const Function A member function that can’t change the object Syntax
functionName() const Ex: class employee { private: double salary; public: int ID; string jobtitle; double getSalary(){ salary *= 1.5; return salary; } double getSalary() const{ salary *= 1.5; // not allow: complier error };
10
Class Constructors A function that is automatically called when a class instance (object) is created or declared It does NOT have any return value Ex: #include <iostream> #include <string> using namespace std; class employee { private: double salary; public: int ID; string jobtitle; employee(){ ID = 1; jobtitle = “student”; salary = 0; } };
11
Class Constructors Constructors can also have arguments Ex:
#include <iostream> #include <string> using namespace std; class employee { private: double salary; public: int ID; string jobtitle; employee(int e_id, sting pos, double pay){ ID = e_id; jobtitle = pos; salary = pay; } };
12
Class Constructors Multiple Constructors (Overload) Ex: Default
class employee { private: double salary; public: int ID; string jobtitle; employee(){ ID = 1; jobtitle = “student”; salary = 0; } employee(int e_id, sting pos, double pay){ ID = e_id; jobtitle = pos; salary = pay; }; Default Constructor Overloaded Constructor
13
Practice Time! Worksheet 4 Make teams of 2 people
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.