Module - 2.12
Module - 2.12
COURSE MODULE IN
CCCS 103 - Intermediate Programming
Bachelor of Science in Information System
2nd Semester S/Y 2020 – 2021
I. Cover Sheet
A. MODULE NUMBER : 2.12
E. INSTRUCTOR :
· Class. is a user-defined data type that we can use in our program, and it works as an object constructor, or a
"blueprint" for creating objects.
Page 1 of 14
Republic of the Philippines
Camarines Sur Polytechnic Colleges
Nabua, Camarines Sur
ISO 9001:2015
Do Pre-Test 00:10
Do Post-Test 00:50
*Check if accomplished
V. Pre-test
True/False: This pre-test will try to assess your knowledge prior to the discussion of the topic. Each item is a
statement for you to evaluate whether true or false. Please put a check on the column that corresponds to your
answer.
__________1.A class is pre-defined data type which serves as the blueprint for creating objects.
__________2.Variables and methods of a class can always be accessed in any part of the program.
__________3.A method declared within a class can also be defined outside the class using the symbol ::.
__________4.Class members marked as private can only be accessed by functions defined within the class.
__________5.Inheritance is practiced when reusing attributes and methods of an exiting class to a newly
defined class.
__________6.Object-Oriented Programming (OOP) reduces the number of lines of code and reduces
development time.
__________7.To access public member of the class we use comma (,) operator.
__________8.Attributes and methods are basically variables and functions that belongs to the class.
__________9.Polymorphism is a method of inheriting attributes and methods from another class.
_________10.Encapsulation is a property of a class which make sure that "sensitive" data is hidden from users.
Procedural programming is about writing procedures or functions that perform operations on the data, while object-
oriented programming is about creating objects that contain both data and functions.
Page 2 of 14
Republic of the Philippines
Camarines Sur Polytechnic Colleges
Nabua, Camarines Sur
ISO 9001:2015
OOP helps to keep the C++ code DRY "Don't Repeat Yourself", and makes the code easier to maintain,
modify and debug
OOP makes it possible to create full reusable applications with less code and shorter development time
What is a Class?
A C++ class combines data and methods for manipulating the data into one. Classes also determine the forms of
objects. The data and methods contained in a class are known as class members. A class is a user-defined data type. To
access the class members, we use an instance of the class. You can see a class as a blueprint for an object.
A class be a prototype for a house. It shows the location and sizes of doors, windows, floors, etc. From these
descriptions, we can construct a house. The house becomes the object. It's possible to create many houses from the
prototype. Also, it's possible to create many objects from a class.
In the above figure, we have a single house prototype. From this prototype, we have created two houses with different
features.
Everything in C++ is associated with classes and objects, along with its attributes and methods. For example:
in real life, a car is an object. The car has attributes, such as weight and color, and methods, such as drive and
brake.
Attributes and methods are basically variables and functions that belongs to the class. These are often referred
to as "class members".
A class is a user-defined data type that we can use in our program, and it works as an object constructor, or a
"blueprint" for creating objects.
Class Declaration
In C++, a class is defined using the class keyword. This should be followed by the class name. The class body is then
added between curly braces { }.
Syntax:
Page 3 of 14
Republic of the Philippines
Camarines Sur Polytechnic Colleges
Nabua, Camarines Sur
ISO 9001:2015
The data is the data for the class, normally declared as variables.
You must have come across these two keywords. They are access modifiers/ access specifier. Access specifiers define
how the members (attributes and methods) of a class can be accessed.
Private:
When the private keyword is used to define a function or class, it becomes private. Such are only accessible
from within the class.
Public:
The public keyword, on the other hand, makes data/functions public. These are accessible from outside the
class.
Class Methods
In the following example, we define a function inside the class, and we name it "myMethod".
int main() {
MyClass myObj; // Create an object of MyClass
myObj.myMethod(); // Call the method
return 0;
}
To define a function outside the class definition, you have to declare it inside the class and then
define it outside of the class. This is done by specifiying the name of the class, followed the scope
resolution :: operator, followed by the name of the function.
Object Definition
Objects are created from classes. Class objects are declared in a similar way as variables are declared. The class name
must start, followed by the object name. The object of the class type.
Syntax:
The class-name is the name of the class from which an object is to be created.
To access public members of a class, we use the (.)dot operator. These are members marked with public access
modifier.
Example #1:
Output:
Page 5 of 14
Republic of the Philippines
Camarines Sur Polytechnic Colleges
Nabua, Camarines Sur
ISO 9001:2015
Code Explanation:
1. Include the iostream header file in our code in order to use its functions.
2. Including the std namespace in our code to use its classes without calling it.
3. Declare a class named Phone.
4. Using the public access modifier to mark the variables we are about to create as publicly accessible.
5. Declare the variable cost of a double data type.
6. Declare an integer variable named slots.
7. End of the class body.
8. Calling the main()function. The program logic should be added within its body.
9. Create an object named Y6 of type Phone. This is called instantiation.
10. Create an object named Y7 of type Phone. This is called instantiation.
11. Access the variable/member cost of class Phone using the object Y6. The value is set to 100.0. The cost of Y6
is now set to 100.0.
12. Access the variable/member slots of class Phone using the object Y6. The value is set to 2. The slots for Y6 is
now set to 2.
13. Access the variable/member cost of class Phone using the object Y7. The value is set to 200.0. The cost of Y7
is now set to 200.0.
14. Access the variable/member slots of class Phone using the object Y7. The value is set to 2. The slots for Y7 is
now set to 2.
15. Print the cost of Y6 on the console alongside other text.
16. Print the cost of Y7 on the console alongside other text.
17. Print the number of slots for Y6 alongside other text.
18. Print the number of slots for Y7 alongside other text.
19. The program must return a value upon successful completion.
20. End of the body of main() function.
Class members marked as private can only be accessed by functions defined within the class. Any object or function
defined outside the class cannot access such members directly. A private class member is only accessed by member
and friend functions.
Class members marked as protected have an advantage over those marked as private. They can be accessed by
functions within the class of their definition. Additionally, they can be accessed from derived classes.
Example #2
Page 6 of 14
Republic of the Philippines
Camarines Sur Polytechnic Colleges
Nabua, Camarines Sur
ISO 9001:2015
Output:
Code Explanation:
1. Include the iostream header file in our code to use its functions.
2. Include the std namespace in our code to use its classes without calling it.
3. Create a class named ClassA.
4. Use the public access modifier to mark the class member to be created as publicly accessible.
5. Create the function named set_a() that takes one integer value val.
6. Create a function named get_a().
7. Use the private access modifier to mark the class member to be created as privately accessible.
8. Declare an integer variable named a.
9. End of the class body.
10. Use the class name and the scope resolution operator to access the function get_a(). We want to define what
the function does when invoked.
11. The function get_a() should return the value of variable a when invoked.
12. End of the definition of the function get_a().
13. Use the class name and the scope resolution operator to access the function set_a(). We want to define what
the function does when invoked.
14. Assigning the value of the variable val to variable a.
15. End of definition of the function set_a().
16. Call the main() function. The program logic should be added within the body of this function.
17. Create an instance of ClassA and give it the name a.
18. Use the above class instance and the function set_a() to assign a value of 20 to the variable a.
19. Printing some text alongside the value of variable a on the console. The value of variable a is obtained by
calling the get_a() function.
20. The program must return value upon successful completion.
Page 7 of 14
Republic of the Philippines
Camarines Sur Polytechnic Colleges
Nabua, Camarines Sur
ISO 9001:2015
Functions help us manipulate data. Class member functions can be defined in two ways:
If a function is to be defined outside a class definition, we must use the scope resolution operator (::). This should be
accompanied by the class and function names.
Example #3:
Output:
Code Explanation:
Page 8 of 14
Republic of the Philippines
Camarines Sur Polytechnic Colleges
Nabua, Camarines Sur
ISO 9001:2015
19. Use the guru99 instance to assign a value of 1001 to the variable id.
20. Use the instance guru99 to call the function printname() .
21. Call the end (end line) command to print a new blank line on the console.
22. Use the instance guru99 to call the function printid().
23. The program must return value upon successful completion.
24. End of the body of main() function.
What is Constructors?
Constructs are special functions that initialize objects. The C++ compilers calls a constructor when creating an object.
The constructors help to assign values to class members. Of course, this is after they have been allocated some
memory space.
What is Destructors?
The constructor name must be similar to the class name. Constructors do not have a return type.
The constructor can be defined inside or outside the class body. If defined outside the class body, it should be defined
with the class name and the scope resolution operator (::).
Output:
Page 9 of 14
Republic of the Philippines
Camarines Sur Polytechnic Colleges
Nabua, Camarines Sur
ISO 9001:2015
Code Explanation:
1. Include the iostream header file into the code to use its functions.
2. Include the std namespace in our code to use its classes without calling it.
3. Create a class named ClassA.
4. Use the public access modifier to mark the member we are about to create as publicly accessible.
5. Create a constructor for the class.
6. Text to print on the console when the constructor is called. The endl is a C++ keyword, which means end
line. It moves the mouse cursor to the next line.
7. End of the body of the class constructor.
8. Create a destructor for the class.
9. Text to print on the console when the destructor is called. The endl is a C++ keyword, which means end line.
It moves the mouse cursor to the next line.
10. End of the body of the destructor.
11. End of the class body.
12. Call the main() function. The program logic should be added within the body of this function.
13. Create a class object and give it the name a. The constructor will be called.
14. Create an integer variable named p and assign it a value of 1.
15. Create an if statement block using the variable p.
16. Create a class object and give it the name b. The destructor will be called.
17. End of the body of the if statement.
18. End of the body of the main() function.
Encapsulation
The meaning of Encapsulation, is to make sure that "sensitive" data is hidden from users. To achieve this, you
must declare class variables/attributes as private (cannot be accessed from outside the class). If you want
others to read or modify the value of a private member, you can provide public get and set methods.
Access Private Members
It is considered good practice to declare your class attributes as private (as often as you can). Encapsulation
ensures better control of your data, because you (or others) can change one part of the code without affecting
other parts
Inheritance
In C++, it is possible to inherit attributes and methods from one class to another. We group the "inheritance concept"
into two categories:
derived class (child) - the class that inherits from another class
- It is useful for code reusability: reuse attributes and methods of an existing class when you create a new class.
Page 10 of 14
Republic of the Philippines
Camarines Sur Polytechnic Colleges
Nabua, Camarines Sur
ISO 9001:2015
Polymorphism
Polymorphism means "many forms", and it occurs when we have many classes that are related to each other
by inheritance.
Like we specified in the previous definition; Inheritance lets us inherit attributes and methods from another
class. Polymorphism uses those methods to perform different tasks. This allows us to perform a single action
in different ways.
VII. References
1. W3schools. 2021. C++ Classes and Objects. https://www.w3schools.com/cpp/cpp_classes.asp
2. Guru99. 2021. C++ Class and Object with Example. https://www.guru99.com/cpp-classes-objects.html
VIII. Assignment/Activity
Instruction:
Create a C++ program that calculates a group of students' quiz scores. To determine the number of times to
repeat instruction on inputting students' raw scores, the program must first determine the total number of
students. Then ask to input for the Total Marks. The formula to compute for the score is raw_score/ totat_marks *
50 + 50. Follow specific instructions below.
b. Under Class student, create private variables for student_name, total_marks and total_score.
c. Under Class student, Create a function getDetails which accepts student_name and student_raw
score as an input and function putDetails to display the total_score
Sample output:
Page 11 of 14
Republic of the Philippines
Camarines Sur Polytechnic Colleges
Nabua, Camarines Sur
ISO 9001:2015
Submission of outputs will be either of the following based on your level of technology:
High Level: Screenshot/picture of written/printed code and output to be uploaded to CSPC LeOns.
Middle Level: Screenshot/picture of written/printed code and output to be uploaded to CSPC LeOns.
Low Level: Written/printed code and output that will be collected on designated place and time.
CRITERIA RATING
Page 12 of 14
Republic of the Philippines
Camarines Sur Polytechnic Colleges
Nabua, Camarines Sur
ISO 9001:2015
Instruction:
The basic structure of Objects and Classes is shown on program below, it contains line of code that may cause a
program error, encircle them and rewrite the program.
Submission of outputs will be either of the following based on your level of technology:
Low Level: Written/printed code and output that will be collected on designated place and time.
Page 13 of 14
Republic of the Philippines
Camarines Sur Polytechnic Colleges
Nabua, Camarines Sur
ISO 9001:2015
VIII. Post-Test
True/False: This post-test will try to assess your knowledge after the discussion of the topic. Each item is a
statement for you to evaluate whether true or false. Please put a check on the column that corresponds to your
answer.
__________1.A class is pre-defined data type which serves as the blueprint for creating objects.
__________2.Variables and methods of a class can always be accessed in any part of the program.
__________3.A method declared within a class can also be defined outside the class using the symbol ::.
__________4.Class members marked as private can only be accessed by functions defined within the class.
__________5.Inheritance is practiced when reusing attributes and methods of an exiting class to a newly
defined class.
__________6.Object-Oriented Programming (OOP) reduces the number of lines of code and reduces
development time.
__________7.To access public member of the class we use comma (,) operator.
__________8.Attributes and methods are basically variables and functions that belongs to the class.
__________9.Polymorphism is a method of inheriting attributes and methods from another class.
_________10.Encapsulation is a property of a class which make sure that "sensitive" data is hidden from users.
***please answer this post-test so that you will be able to determine if there is a significant difference on your
score compared to your pre-test.
Submission of your post-test will be either of the following based on your level of technology:
High and Middle Level: Screenshot/picture of written/printed output to be uploaded to CSPC LeOns
Low Level: Written/printed output that will be collected on designated place and time.
Page 14 of 14