Lecture 10 - Classes 1
Lecture 10 - Classes 1
Classes-I
Introduction
• Object-oriented programming (OOP)
– Encapsulates data (attributes) and functions (behavior) into packages
called classes
• Information hiding
– Implementation details are hidden within the classes themselves
• Classes
– Classes are the standard unit of programming
– A class is like a blueprint – reusable
– Objects are instantiated (created) from the class
– For example, a house is an instance of a “blueprint class”
34 return 0;
35 }
7 #define TIME1_H
8
If time1.h (TIME1_H) is not defined (#ifndef)
9 // Time abstract data type definition then it is loaded (#define TIME1_H). If TIME1_H
is already defined, then everything up to #endif is
10 class Time {
ignored.
11 public: This prevents loading a header file multiple times.
12 Time(); // constructor
13 void setTime( int, int, int ); // set hour, minute, second
14 void printMilitary(); // print military time format
15 void printStandard(); // print standard time format
16 private:
17 int hour; // 0 - 23
18 int minute; // 0 - 59
19 int second; // 0 - 59
20 };
21
22 #endif
23 // Fig. 6.5: time1.cpp
24 // Member function definitions for Time class.
25 #include <iostream>
26
27 using std::cout;
28 Source file uses #include to load
29 #include "time1.h" the header file
30
31 // Time constructor initializes each data member to zero.
32 // Ensures all Time objects start in a consistent state.
33 Time::Time() { hour = minute = second = 0; }
34
35 // Set a new Time value using military time. Perform validity
36 // checks on the data values. Set invalid values to zero.
37 void Time::setTime( int h, int m, int s )
38 {
39 hour = ( h >= 0 && h < 24 ) ? h : 0;
40 minute = ( m >= 0 && m < 60 ) ? m : 0;
41 second = ( s >= 0 && s < 60 ) ? s : 0; Source file contains function
42 } definitions
43
44 // Print Time in military format
45 void Time::printMilitary()
46 {
47 cout << ( hour < 10 ? "0" : "" ) << hour << ":"
48 << ( minute < 10 ? "0" : "" ) << minute;
49 }
50
51 // Print time in standard format
52 void Time::printStandard()
53 {
54 cout << ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 )
55 << ":" << ( minute < 10 ? "0" : "" ) << minute
56 << ":" << ( second < 10 ? "0" : "" ) << second
57 << ( hour < 12 ? " AM" : " PM" );
58 }
Lecture 10: Classes-I CS 112: Programming Techniques
Compiling...
Fig06_06.cpp
D:\Fig06_06.cpp(15) : error C2248: 'hour' : cannot access private
member declared in class 'Time'
D:\Fig6_06\time1.h(18) : see declaration of 'hour'
D:\Fig06_06.cpp(18) : error C2248: 'minute' : cannot access private
member declared in class 'Time'
D:\time1.h(19) : see declaration of 'minute'
Error executing cl.exe.
• Following example
– Program to take in monthly sales and output the total
– Implementation not shown, only access functions
• In this quiz you have to write a solution for course record system
of GIK. The university mentions a list of students consisting of
their Roll No., Name, Age, Current Semester, CGPA, Faculty,
Date of Birth, Address, Cell No and Courses (it will be a list of
5 courses). Each course will have its Title, Credit hours and
Course Code.
References
• Book Code: A