Amos C++ Lab 3
Amos C++ Lab 3
Amos C++ Lab 3
THEORY
C++ is the universal language for programmers. It is the language programmers use to share
ideas with one another. Basically, C++ involves three important steps these include: entering of
the designed programme, compiling of the programme and executing or running of the
programme. It is a general-purpose object-oriented programming (OOP) language, developed
by Bjarne Stroustrup, and is an extension of the C language. It is therefore possible to code C++
in a "C style" or "object-oriented style." In certain scenarios, it can be coded in either way and is
thus an effective example of a hybrid language. C++ is considered to be an intermediate-level
language, as it encapsulates both high- and low-level language features. Initially, the language
was called "C with classes" as it had all the properties of the C language with an additional
concept of "classes." However, it was renamed C++ in 1983. It is pronounced "see-plus-plus."
However, this lab experiment is entirely based on the introduction to ordinary classes in C++
programming C++ classes (and often function prototypes) are normally split up into two files as
will be illustrated in this practical. The header file has the extension of .h and contains class
definitions and functions. The implementation of the class goes into the .cpp file. By doing this,
if your class implementation doesn’t change then it won’t need to be recompiled. Most IDE’s
will do this for you – they will only recompile the classes that have changed. This is possible
when they are split up this way, but it isn’t possible if everything is in one file (or if the
implementation is all part of the header file).
DESIGN
For efficient operation of c++ program, the following algorithms were designed for the given
exercise.
1. # include <iostream> - purposely designed to give instruction to the compiler and thus
shows the useful library for the program.
2. Using name space std; - designed to introduce an output code, the return value for the
particular input to be used.
3. Int main ( ) – Shows the start of executable instructions.
4. { - this indicates the starts of operation of the main function.
5. Cout - to output instructions to the screen.
6. << - to show the direction of data movement from r-value to the console.
11. Endl - designed to enable data to be printed on more than one line.
12. Std ::cout - prints data to the console using the output operator <<
13. Std ::cin – reads inputs from the user at the console using the input operator>>
PROCEDURE
PRACTICAL 1: USE OF ORDINARY CLASSES
We were required to use a C++ function which computes the factorial of any positive integer
greater than zero. In order to achieve this, we made use of C++ loop constructs (for-loop, or
while-loop), escape sequences: \n, \t, etc. and characters such as: ‘-‘and ‘|’ to build rows and
columns of the table. The sample table is attached in the discussion below.
Step 2 - Click on “Create a new project”; this will open the following window:
Step 5 - Fill in details about my project: title “cpp_lab06”and folder (location of my choice)
and click on “Next”; and this opened the following window:
Step 6 – I Click on “Finish” and I was presented with a window similar to the one below,
Step 7 – I Click on next to the folder “Sources” and I was presented with the following window
I was presented with a window below; I name the header file as “studenteng.h” and browsed
to the project directory as its location and click “Finish”.
1. Step 1 - A new project called “cpplab06b was started and went through the steps of
initialising project files.
2. A header file “shape.h” was created in which we declared a class shape and copied the code
from the introduction above. In addition, we declared three classes: Square, Circle and Triangle,
all of them inheriting from shape and implementing abstract functions computeArea() and
draw().
3. Ensure that constructors of derived classes (Square, Circle and Triangle) take string
“shapeName” as their parameter and initialise the member variable with that argument.
4. In the draw () function of derived classes, we printed the message “drawing the shape:
‘shapeName’”.
5. In main function, we declared three shape objects which we initialised to the instance of
Square, Circle and Triangle respectively.
6. We set width and height or radius (in case of circle) and called functions computeArea() and
draw() on these three instances of shapes; one at a time and recorded all results
RESULTS
PRACTICAL 1
C++ PROGRAM
Compiled
Compiled
Compiled
Running`
PRACTICAL 2
Compiled
Compiled
Running
DISCUSSION
For practical one, we were instructed to make an ordinary class under the name UnzaStudent.
This class member variables such as first name and age to say the least, which were declared as
private member variables. Under public, functions were declared in the form of set and get
whose sole purpose was to let the program access the private member variables. The first thing
that was then done was to make a main, header as well as source files in which this program
was going to be worked on. The creation of the header and source file was easily done with a
code blocks facility that allows the programmer to automatically make a class as well as its
source and header files, as well as a constructor. So, in general all that was done in the header
file was to declare the class and its member variables and function prototypes. An additional
function prototype called display made in the source file created a table in which these
variables were put. The source file was being the function bodies were made. And the set and
get functions were implemented, returning the desired variable. And then finally in the main
program the program promoted the user to input data such as age, computer number etc. Then
the constructor which initiated the member variables was called or used. And the get functions
also called for implementation. The display function was also called and the program ran
smoothly.
For practical 2, the use of abstract classes was implemented. A class with the name shape was
declared in the header, and a string called ‘shapeName’, and integers called width and height
were declared under the protected access specifier. Under the public access specifier, a virtual
integer identifier was declared under the name ‘computeArea’ and was initialized to 0. Also the
virtual functions called draw, setWidth, setHeight and shapeName were declared, with the
draw function initialized at 0 also
APPLICATION
C++ is applied in many areas of life.eg
Operating Systems:
C++ forms an integral part of many of the prevalent operating systems including Apple’s OS X
and various versions of Microsoft Windows, and the erstwhile Symbian mobile OS.
Enterprise Software:
C++ finds a purpose in banking and trading enterprise applications, such as those deployed by
Bloomberg and Reuters. It is also used in development of advanced software, such as flight
simulators and radar processing
CONCLUSION
The class is the foundation of C++’s support for object-oriented programming, and is at the core
of many of its more advanced features. The class is C++’s basic unit of encapsulation and it
provides the mechanism by which objects are created.
In general, when programming in an object-oriented fashion, you decompose a problem into its
constituent parts. Each component becomes a self-contained object that contains its own
instructions and data related to that object. Through this process, complexity is reduced and
you can manage larger programs.
REFERENCES
1. Mostafa A.E.B.,et al FUNDAMENTALS OF COMPUTER ORGANIZATION AND ARCHITECTURE .,
(2005),John Wiley and Sons Inc,Hoboken ,new jersey, Canada.