Act 1 - Review of CPP Programming
Act 1 - Review of CPP Programming
1
REVIEW OF C++ PROGRAMMING
Course Code: CPE010 Program: Computer Engineering
Course Title: Data Structures and Algorithms Date Performed:
Section: Date Submitted:
Name: Instructor:
1. Objective(s)
Implement basic programming and OOP in C++
2. Intended Learning Outcomes (ILOs)
After this module, the student should be able to:
a. Create code that follows the basic C++ code structure;
b. Implement appropriate class definition and instances based on given requirements;
c. Solve different problems using the C++ programming language.
3. Discussion
Global Declaration
int count = 0;
Section
Class Declaration
class rectangle{
and Method private:
Definition Section double recLength, recWidth;
public:
rectangle(double L, double W);
void setLength(double L);
void setWidth(double W);
double getPerimeter();
};
Main Function
int main(){
rectangle shape1(2, 5);
std::cout << "The perimeter of the rectangle is " << shape1.getPerimeter() <<
".\n";
std::cout << count << " number of objects created."; return 0;
Method Definition
rectangle::rectangle(double L, double W) {
recLength = L;
recWidth = W;
count++;
}
3
void rectangle::setLength(double L) {
recLength = L;
}
void rectangle::setWidth(double W) {
recWidth = W;
}
double rectangle::getPerimeter() {
return (2*recLength) + (2*recWidth);
}
It is not required for all sections to have code for every use-case. However, for best practices you
would prefer to have an overall structure to follow to increase code readability and reusability.
Data Types
d. Primary Data Type: int, float, char and void
e. User defined data type: structure, union, class, enumeration
f. Derived data type: array, function, pointer, reference
#include <iostream>
using namespace std;
int main(){
int localVal = 5; //Local Variable
std::cout << "Global Variable has value " << globalVal << ".\n"; std::cout <<
"Local Variable has value " << localVal << ".\n";
return 0;
}
Operators
Arithmetic Relational
Addition + Greater than >
Subtraction – Less than <
Multiplication * Greater than or equal >=
Division / Less than or equal <=
Modulo % Equal ==
Increment ++ Not equal !=
Decrement --
Bitwise Operators
Let A = 60 and B = 13. Binary values are as follows:
A = 0011 1100
B = 0000 1101
4
Bitwise AND -> & A&B 0000 1100
Bitwise OR -> | A |B 0011 1101
Bitwise XOR -> ^ A ^B 0011 0001
Bitwise Complement -> ~ ~A 1100 0011
Assignment Operator
class myClass {
public:
int myNum;
string myString;
};
public here is an access specifier. It indicates that the attributes and methods listed under it are accessible
outside the class. A simple table is provided below to summarize the access specifiers used in c++.
int main(){
//this creates the object
myClass object1;
return 0;
}
5
5. Procedure
ILO A: Create Code That Follows the Basic C++ Code Structure
For this activity, you have to demonstrate the use of a function prototype. The section on
class declaration and method definition will be used for the function prototype and the
function will be defined in the follow method definition section after the main function.
A function prototype in c++ is a declaration of the name, parameters and return type of the
function before its definition. Write a C++ code the satisfies the following:
Create a function that will take two numbers and display the sum.
Create a function that will return whether variable A is greater than variable B.
Create a function that will take two Boolean values and display the result of all
logical operations then return true if it was a success.
Note:
The driver program must call each function.
The definitions must be after the main function.
ILO B: Implement Appropriate Class Definition and Instances Based on Given Requirements
In this section, the initial implementation for a class triangle will be implemented. The step-
by-step procedure is shown below:
Step 1. Include the necessary header files. For this one, we only need #include <iostream> Step 2.
Create the triangle class. Assign it with private variables: totalAngle, angleA, angleB, and
angleC.
class Triangle{
private:
double totalAngle, angleA, angleB, angleC;
Step 3. We then create public methods. The constructor must allow for creation of the object
with 3 initial angles to be stored in our previously defined variables angleA, angleB and
angleC. Another method has to be made if the user wants to change the initial values, this
will also accept 3 arguments to change the values in angleA, angleB and angleC. Lastly, a
function to validate whether the given values make our shape an actual triangle.
public:
Triangle(double A, double B, double C);
void setAngles(double A, double B, double C); const
bool validateTriangle();
};
6
angleB = B;
angleC = C;
totalAngle = A+B+C;
}
angleB = B;
angleC = C;
totalAngle = A+B+C;
}
int main(){
//driver code
Triangle set1(40, 30, 110);
if(set1.validateTriangle()){
std::cout << "The shape is a valid triangle.\n"; } else {
return 0;
}
Include the output of running this code in section 6. Note your observations and comments.
6. Output
Sections Answer
Header File Declaration Section
Global Declaration Section
Class Declaration and
Method Definition Section
Main Function
Method Definition
Table 1-1. C++ Structure Code for Answer
7
7. Supplementary Activity
ILO C: Solve Different Problems using the C++ Programming Language
The supplementary activities are meant to gauge your ability in using C++. The problems below range from
easy to intermediate to advanced problems. Note your difficulties after answering the problems below.
8. Conclusion
9. Assessment Rubric