You are on page 1of 9

LAB-01

INTRODUCTION TO C++ AND REVIEW OF PROGRAMMING


CONCEPTS

OBJECTIVES :

 Introduction to C++ language and its syntax and its coding on codeblocks IDE.
 Relation of C and C++ language.
 Structures in C++ and its syntax and basic concepts about it.
 Difference between passing arguments by value and by reference in C++.
 C++ have compatibility with C. Virtually, every error-free C program is a valid C++
program. Depending on the compiler used, every program of C++ can run on a file
with .cpp extension..

INTRODUCTION.:

 C++ is derived from the C language, it is a superset of C:


 It adds to the C language the capability to implement OOP. It also adds a variety of other features.
some features common to C, although still available in C++, are seldom used
 C++ offers the feature of portability or platform independence which allows the user
to run the same program on different operating systems or interfaces at ease.
So we can programs on Linux, Unix , Windows and MAC OS as well. This feature
proves to be of great convenience to the programmer.

IN LAB TASKS

TASK # 1 :

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.

 TASK CODE :

1 #include <iostream> //Standard library


2 using namespace std;
3 struct date_of_birth //structure declaration
4 {
5 int date; //variable 1 (date in integer)
6
7
char month[]; //variable 2 (month in character)
8
int year; //variable 3 (year in integer)
9
};
10
11
int main() //Main function
12
{
13
date_of_birth P1; //Declaration of object of structure Person
14
cout << "Enter date: ";
15
cin >>P1.date; //Passing data to date variable of Person
16
cout << "\nEnter month : ";
17
cin >>P1.month; //Passing data to month variable of Person
18
cout << "\nEnter year : ";
19
cin >> P1.year; //Passing data to year variable of Person
20
21
//To Display data Entered which was stored in variables
22
cout<< "\nDate of Birth in format dd//mm//yyyy is ";
23
cout << " " <<P1.date;
24
cout << " / " <<P1.month;
25
cout << " / " <<P1.year;
26
cout << "\n\n";
27
28
return 0;
29
}

 OUTPUT :

TASK # 2 :

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.

 TASK CODE :
1 #include <iostream> //Standard library
2 using namespace std;
3
4 struct student_data //Declaration of structure
5 {
6 //Declaration of variables and names
7
int age ;
8
char name[25] ;
9
int reg_no;
10
};
11
12
int main()
13
{
14
int i; //Declaration of for loop counter
15
student_data P[3];
16
for(i=1 ; i<=3 ; i++) //For loop for array to take data for 3 students
17
{
18
//Taking inputs for 3 students
19
cout << "Student " << i <<" data";
20
21
cout << "\nEnter student name : ";
22
cin >> P[i].name;
23
cout << "Enter student age : ";
24
cin >> P[i].age;
25
cout << "Enter roll number : ";
26 cin >> P[i].reg_no;
27 cout << "\n\n";
28 }
29 for(i=1;i<=3;i++) //for loop to display records
30 {
31 cout <<"Student " <<i <<" data Record Show"<<endl;
32 cout <<"Name : "<<P[i].name<<endl;
33 cout <<"Age : "<<P[i].age<<endl;
34 cout <<"Registration Number : "<<P[i].reg_no<<endl;
35 }
36 return

 OUTPUT :
TASK # 3 :

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.

 TASK CODE :

1 #include<iostream> //Standard library


2 using namespace std;
3 struct book //Declaration of structure
4 { //Declaration of variable type and name
5 char name[50];
6 float price;
7 int pages;
8 };
9 int main()
10 {
11 int i,loop_counter; //Declaration of variable for for loop and array
12 cout<<"Enter number of books you want to enter data for :";
13 cin>>i; //Input for length of array
14 book B[i];
15
16 for(loop_counter=0;loop_counter<i;loop_counter++)
17 {
18 //loop_counter will be used as array index
19 cout<<"\n\nData for Book "<<loop_counter;
20 cout<<"\nEnter name :";
21 cin>>B[loop_counter].name;
22 cout<<"Enter price of book :";
23 cin>>B[loop_counter].price;
24 cout<<"Enter pages of book :";
25 cin>>B[loop_counter].pages;
26 cout<<"\n";
27
28 //Displaying information
29 }
30 for(loop_counter=0;loop_counter<i;loop_counter++)
31 {
32 //Checking price of every book at book on index 0
33 //if price of book at index 0 is less than the book [1] then if condition continues
34 if( B[0].price < B[loop_counter].price )
35 B[0].price = B[loop_counter].price; //higher price will be stored in book[0]
36 }
37 cout << "Largest price is of book = " << B[0].name<<"is most costly ";
38 }

 OUTPUT :

TASK # 4 :

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

 TASK CODE :

1 #include<iostream>
2 using namespace std;
3
4 int swap_by_reference(int &,int &);
5 int swap_by_value(int &,int &);
6 int main()
7 {
8 int num_1,num_2;
9 cout<<"Enter Number 1 :";
10
cin>>num_1;
11
cout<<"Enter Number 2 :";
12
cin>>num_2;
13
14
cout<<"\nBefore swapping : \nNumber 1 = "<<num_1<<"\nNumber 2 =
15
"<<num_2<<endl;
16
17
swap_by_reference(num_1,num_2);
18
swap_by_value(num_1,num_2);
19
}
20
int swap_by_reference(int & x,int & y)
21
{
22
int a;
23
a=x;x=y;y=a;
24
cout<<"\nAfter swapping by reference : \nNumber 1 = "<<x<<"\nNumber 2 = "<<y;
25
}
26
27
int swap_by_value(int& a,int & b)
28
{
29
int d;
30
d=a;a=b;b=d;
31
cout<<"\nAfter swapping by value : \nNumber 1 = "<<a<<"\nNumber 2 = "<<b;
32
cout<<"\n\n";
33
}
34

 OUTPUT :

POST LAB TASKS


TASK # 1 :

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

 TASK CODE :

1 #include<iostream> //standard library


2 using namespace std;
3
4 struct employee //Declaring structure
5 {
6 //Declaring variables types and their names (line 6_8)
7 int code;
8 char name[50];
9 int month_of_joining;
10 int date_of_joining;
11 int year_of_joining;
12 };
13 int main()
14 {
15 int i,current_year,current_date,current_month,loop_counter ;
16 //I for array length and loop counter for for loop
17 cout<<"Enter number of employees you want to enter data for :";
18 cin>>i; //Taking input for array length
19 employee E[i];
20
21 for(loop_counter=1;loop_counter<=i;loop_counter++) //to take input in an array
22 {
23 //Taking inputs in array (line 18-24)
24 cout<<"\n\nData for Employee "<<loop_counter;
25 cout<<"\nEnter code :";
26 cin>>E[loop_counter].code;
27 cout<<"Enter name :";
28 cin>>E[loop_counter].name;
29 cout<<"Enter date of joining (1-31):";
30 cin>>E[loop_counter].date_of_joining;
31 cout<<"Enter month of joining (1-12):";
32 cin>>E[loop_counter].month_of_joining;
33 cout<<"Enter year of joining:";
34 cin>>E[loop_counter].year_of_joining;
35 cout<<"\n";
36 }
37 cout<<"Enter current date (1-31) ";
38 cin>>current_date;

39 cout<<"Enter current month ";


40
cin>>current_month;
41
cout<<"Enter current year ";
42
cin>>current_year;
43
44
// we have to show name for employees whose tenure is 3 or more than 3
45
cout<<"\nEmployees whose tenure is 3 or more than 3 years are :";
46
47
for(loop_counter=1;loop_counter<=i;loop_counter++)
48
{
49
//if statement proceeds only when the difference is equal to 3 or greater than
50
if(current_year-E[loop_counter].year_of_joining>3)
51
{
52
//When the year difference is greater than 3 no need to check month & date
53
cout<<"\n "<<E[loop_counter].name<<endl;
54
}
55
//If year difference is equals to 3 we check for month and date too
56
//so for this we use logical operators
57
//if any condition doesnot satisfy if statement does not run
58
59
if((current_year-E[loop_counter].year_of_joining==3)&&
60
(current_month-E[loop_counter].month_of_joining>=0)&&
61
current_date-E[loop_counter].date_of_joining>=0 )
62
{
63
cout<<"\n "<<E[loop_counter].name<<endl;
64
}
65
}
66
}

 OUTPUT :

LAB CONCLUSION :
 Understanding the logics of programs and implementing in C++ coding to design a
program.
 We work on detail about structure that is structure is a collection of simple variables.
The variables in a structure can be of different types: Some can be int, some can be
float, and so on.The data items in a structure are called the members of the structure.
 Revision of c programming concepts and their use in C++
 Functions are one of the fundamental building blocks of C++ so we revised again the
concepts of functions.
 Passing arguments to function by value and by reference and understanding the
difference between them.
.

------------------------------------------------------------------------------------

You might also like