LAB#01
Introduction to C++ and Review of Basic Programming Concepts
LAB OBJECTIVE:
Objective of this lab is to review basic programming concepts, understanding C++ syntax ,
getting familiar with the concepts of structures and usage of Code Blocks IDE.
INTRODUCTION:
C++ is a general purpose programming language that supports various computer programming
models such as object-oriented programming and generic programming. It was created by Bjarne
Stroustrup and,
“Its main purpose was to make writing good programs easier and more pleasant for the
individual programmer.”
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.
IN-LAB TASKS
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> // store functions(such as built in ascin,cout)
using namespace std;//contain onjects
struct date // (date is class name)& (struct is class)
int date,month,year; //data types for class
};
int main()
{
date d1;//d1 is object
cout<<" Enter Date ";
cin>>d1.date;
cout<< "\n Enter maonth ";//(\n for entering a new line)
cin>> d1.month;
cout<<"\n Enter year ";
cin>>d1.year;
cout << "\n Displaying the format required ";
cout<<"\n "<<d1.date<<"/"<<d1.month<<"/"<<d1.year<<endl;//(endl is
also for entering a new line after executing this)
console window
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> // store functions(such as built in ascin,cout)
using namespace std; //contain onjects
struct data // (data is class name)& (struct is class)
{
char name[50];
int age;
int roll; //different data types for class
};
int main()
{
data d[3]; //d is object of class
for(int i=0;i<=2;i++){
//iterate for loop to take information of three diferent students
cout<<" Enter name";
cin>>d[i].name;
cout<< "\n Enter age ";
//cin for receiving data from user cout for output
cin>> d[i].age;
cout<<"\n Enter roll.no ";
cin>>d[i].roll;
}
for(int a=0;a<=2;a++){
//this for loop is used to display information of three diferent students.
cout << "\n Displaying the student information ";
cout<<"\n name "<<d[a].name<<"\n age "<<d[a].age<<"\n roll.no
"<<d[a].roll;
//this will give us the output
}
}
console window
Task#03
Write a program that declares a structure to store book name, price and pages of a book. The
structure should include functions to assign user defined values to each book and display the
record of most costly book.
Code:
#include<iostream>// contain functions(such as built in as cin,cout)
using namespace std;//contain objects
struct book // (book is class name)& (struct is class)
{
char name[39];
int price; //data of class with datatypes
int pages;
void get(){ // function for properties of the book
cout<<"Name ";
cin>> name;
cout<< "pages";
cin>>pages;
cout<<"prices";
cin>>price;
};
int main()
{
book b[3];//b is an object
float max=0;
for(int i=0;i<3;i++){
b[i].get(); // using for loop , and calling a function inside a
loop to get 3 different books properties.
}
// if statement is used to check which book is expensive.
if((b[0].price>b[1].price)&&(b[0].price>b[2].price)) //if this
condition is true it will return us book 1 name
cout<<"The highest price book name is "<<b[0].name<< " and price is
"<< b[0].price;
else if((b[2].price>b[1].price)&&(b[2].price>b[0].price)) //if this
condition is true it will return us book 3 name
cout<<"The highest price book name is "<<b[2].name<< " and price is
"<< b[2].price;
else if((b[1].price>b[2].price)&&(b[1].price>b[0].price)) //if this
condition is true it will return us book 2 name
cout<<"The highest price book name is "<<b[1].name<< " and price is
"<< b[1].price;
}
console window
Task#04
Write a function that swaps the values of two integer variables
a. using pass by value
b. and pass by reference and see their differences
Code:
#include <iostream>
using namespace std;
// function definition to swap numbers
void svap(int number1, int number2) {
int temp;
temp = number1;
number1 = number2;
number2 = temp;
cout << "a = " << number1 << endl;
cout << "b = " << number2 << endl;
}
// function definition to swap values by references
void swap(int &num1, int &num2) {
int temp;
temp = num1;
num1 = num2;
num2 = temp;
}
int main() {
// initialize variables
int a = 1, b = 2;
cout << "Before swapping" << endl;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
// call function by passing by values
cout << "\nAfter swapping" << endl;
svap(a, b);
// this function call for the values pass by refernces
swap(a, b);
cout << "\nAfter swapping by references" << endl;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
return 0;
}
console window
POST-LAB TASKS
Task#01
There is a structure called employee that holds information like employee code, name, date of
joining. Write a program to create an array of the structure and enter some data into it. Then ask
the user to enter current date. Display the names of those employees whose tenure is 3 or more
than 3 years according to the given current date
Code:
#include <iostream>
using namespace std;
struct employee
{
int emp_code;
char name[50]; //class containing properties of employee
int date_of_joining;
};
int main()
{
int current_date;
int arr[5];
struct employee emp[5];
for(int i=1;i<=3;i++){
//for loop to contain record of 3 employees
cout<<"\nEnter details for employee "<<i<<endl;
cout<<"\nEnter employee code:";
cin>>emp[i].emp_code;
cout<<"\nEnter employee name:";
cin>>emp[i].name;
cout<<"\nEnter employee date of joining:";
cin>>emp[i].date_of_joining;
cout<<"\nEnter current date: ";
cin>>current_date;
int tenure=current_date-emp[i].date_of_joining;
arr[i]=tenure;
}
cout<<"\nNames of employees whose tenure is 3 or more than 3 years
according to the given current date:\n";
for (int i=0;i<5;i++){
if (arr[i]>=3){
cout<<emp[i].name<<endl; //if this condition will become true it
will give us employee name whose tenure is more then or equal to 3 years
}
}
return 0;
}
console window
Conclusion:
# The student will be able to understand difference between C and C++ syntax.
# The student will be able to write simple codes using DEV C++ IDE.
# The student will be able to declare and use structures.
# The student will be able to pass arguments to functions both by value and by reference.
NOTE:font: Times New Roman, Heading font 14, statement font 12