OOP Lab Report 01
OOP Lab Report 01
Lab #: 01
Lab Title: Introduction to C++ and Review of the Basic Programming Concept
Submitted by:
Name Registration #
AMMAR FA19-BCE-001
In-Lab Post-Lab
Modern R9: Understand Tools: Ability to describe and explain the principles
Tools Usage behind and applicability of engineering tools.
In –Lab
Lab#01
By learning C++, you can create applications that will run on a wide variety of hardware
platforms such as personal computers running Windows, Linux, UNIX, and Mac OS X, as well
as small form factor hardware such as IoT devices like the Raspberry PI and Arduino–based
boards.
C++ is derived from the C-Language. Almost every correct statement in C is also correct in C++,
although the reverse is not true.
Basic program:
Line 1: #include <iostream> is a header file library that lets us work with input and output
objects, such as cout (used in line 5). Header files add functionality to C++ programs.
Line 2: using namespace std means that we can use names for objects and variables from the
standard library.
Line 3: A blank line. C++ ignores white space.
Line 4: Another thing that always appear in a C++ program, is int main(). This is called a
function. Any code inside its curly brackets {} will be executed.
Line 5: cout (pronounced "see-out") is an object used together with the insertion operator (<<)
to output/print text. In our example it will output "Hello World".
Line 6: return 0 ends the main function.
The main difference between both these languages is C is a procedural programming language
and does not support classes and objects, while C++ is a combination of both procedural and
object-oriented programming languages.
In procedural programming, each statement in the language tells the computer to do
something: get some input, add these numbers divide by six, display that output. A program in
procedural language is a list of instructions.
For every small program, no other organizing principle is needed. The programmer creates the
list of instructions and the computer carries them out.
In object – oriented programming is to combine into a single unit both data and the function
that operate on that data. Such a unit is called an object. Keep in mind that object – oriented
programming is not primary concerned with the details of program operation, instead it deals
with the overall organization of the program.
Feature C C++
Language Type As mentioned before C is procedural On the other hand, C++ supports both
programming. procedural and object-oriented
programming paradigms.
OOPs feature Support As C does not support the OOPs concept so C++ has support for polymorphism,
it has no support for polymorphism, encapsulation, and inheritance as it is being
encapsulation, and inheritance. an object-oriented programming language
Data Security As C does not support encapsulation so data On another hand in the case of C++
behave as a free entity and can be encapsulation hides the data to ensure that
manipulated by outside code. data structures and operators are used as
intended.
Driven type C in general known as function-driven On the other hand, C++ is known as object
language. driven language.
2.4. Structure:
A structure is a user defined data type. Through structures you have the ability to define a
new type of data considerably more complex than the types we have been using. A structure
is a combination of several different data types. It is similar to a class in that, both holds a
collection of data of different data types. It is declared by using the keyword struct followed
by the structure name.
Syntax:
struct struct_name
{
Data_type1 member_name1;
Data_type2 member_name2;
Data_type3 member_name3;
} object_name;
Difference:
Pass by value Pass by reference
A copy of variable is created and changes are Instead of creating a copy of variable changes
not reflected in original memory address are made at original memory address
3. In-Lab Tasks:
3.1. Task#01: Write a program that declares a structure to store date. Declare an
instance of this structure to represent date of birth. The program should read the day,
month and year values of birth date and display date of birth in dd/mm/yy format.
Code:
#include<iostream>
using namespace std;
3.2. Task#02: Write a program that declares a structure to store Student data containing
his name, age and Roll#. Use array of structures to represent record of 3 students.
Code:
#include<iostream>
using namespace std;
//main function
int main()
{
data d[3]; //creating object in array to
get data of students
return 0;
}
Output:
#include<iostream>
using namespace std;
int main()
{
/*Declaring varible and getting the number of book
user want to enter*/
int n;
cout<<"How many books data you want to enter :";
cin>>n;
}
/*swapping the highest cost book's data to the end
of the array*/
for(int i=0;i<n;i++)
{
if(l[i].price>l[i+1].price)
{
swap(l[i].name,l[i+1].name);
swap(l[i].pages,l[i+1].pages);
swap(l[i].price,l[i+1].price);
}
}
/*printing the last book data from array*/
cout<<endl<<"Most Expensive Book is : ";
cout<<endl<<"name:"<<l[n-1].name<<"\t pages:
"<<l[n-1].pages<<"\t price : "<<l[n-1].price;
return 0;
}
Output:
Code:
#include<iostream>
using namespace std;
int main()
{
//declaring variables and getting data
int a,b,c;
cout<<"Enter the 'a'' value : ";
cin>>a;
cout<<endl<<"Enter the 'b' value : ";
cin>>b;
//swapping by value
if (c==0)
{
swapValue(a,b);
cout<<"\n a = "<<a;
cout<<"\n b = "<<b;
}
//swapping by address
else if (c==1)
{
swapAddress(a,b);
cout<<"\n a = "<<a;
cout<<"\n b = "<<b;
}
return 0;
}
Output:
#include<iostream>
using namespace std;
int main()
{
/*varible declaration to know how many employee's
data user wants to enter*/
int n;
cout<<"How many Employee's Data You want to
enter:";
cin>>n;
/*creating objects*/
employee e[n];
Date doj[n]; //doj=date of joining
Date currentDate;
if((currentDate.year-doj[i].year)>=3)
{
cout<<endl<<"Employee Name :
"<<e[i].name;
}
}
return 0;
}
Output:
5. Conclusion:
In this lab we have seen the importance and use of the structure in C++
Object Oriented Programming (CSC 241) Page 14
programming language, go through the basic syntax of the C++ whereas structures
and practice question related to the implementation of the structures so,
After completing this Lab, we are now able to implement code using structures.