0% found this document useful (0 votes)
124 views24 pages

BPLCK105D - Introduction To C++ Programming - 240328 - 081040

The document provides information about the vision, mission, and objectives of Visvesvaraya Technological University and Acharya Institute of Technology. It outlines the program specific outcomes and program outcomes for various courses. Specifically, it summarizes the course outcomes for Introduction to C++ Programming, covering object-oriented programming concepts, file I/O functions, and exception handling in C++.

Uploaded by

Shreyas C.K
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
124 views24 pages

BPLCK105D - Introduction To C++ Programming - 240328 - 081040

The document provides information about the vision, mission, and objectives of Visvesvaraya Technological University and Acharya Institute of Technology. It outlines the program specific outcomes and program outcomes for various courses. Specifically, it summarizes the course outcomes for Introduction to C++ Programming, covering object-oriented programming concepts, file I/O functions, and exception handling in C++.

Uploaded by

Shreyas C.K
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

VISVESVARAYA TECHNOLOGICAL UNIVERSITY

Belgaum, Karnataka-590 014

Introduction to C++ Programming

Subject Code: BPLCK105D

(As per Visvesvaraya Technological University Syllabus)

B.E- 1st Semester, Information Science and Engineering


Prepared By

Prof. Supriya C Prof. Pushpalatha K S


Assistant Professor Assistant Professor

Reviewed By
Prof. MARY M DSOUZA
Assistant Professor

Approved By:
Prof. Kala Venugopal
Head of Department Information Science and Engineering

ACHARYA INSTITUTE OF TECHNOLOGY


(Affiliated to VTU, Belgaum, Approved by AICTE, New Delhi and Govt. of Karnataka),
Acharya Dr. Sarvepalli Radhakrishnan Road, Bangalore-560107.
Ph: 91-080-28396011, 23723466, 28376431
URL: www.acharya.ac.in
2023-24
Table of contents
Vision, Mission, Motto of Institute I
Vision, Mission of Department II
Laboratory Objectives III
Program Specific Outcomes (PSOs) III
Program outcomes (POs) IV
Course outcomes (COs) VI
MOTTO

"Nurturing Aspirations Supporting Growth" VISION “Acharya Institute of


Technology, committed to the cause of sustainable value-based education in all disciplines,
envisions itself as a global fountainhead of innovative human enterprise, with inspirational
initiatives for Academic Excellence”.

VISION OF THE INSTITUTE

Acharya Institute of Technology, committed to the cause of value-based education in all


disciplines, envisions itself as fountainhead of innovative human enterprise, with inspirational
initiatives for Academic Excellence.

MISSION OF INSTITUTE

“Acharya Institute of Technology strives to provide excellent academic ambiance to the students
for achieving global standards of technical education, foster intellectual and personal
development, meaningful research and ethical service to sustainable societal needs.”
VISION OF THE DEPARTMENT

“To be center of Academic and Research excellence in the field of Information Technology
inculcating value based education for the development of quality Human Resource”

MISSION OF THE DEPARTMENT

“Equip students with fundamental concepts, practical knowledge and professional ethics through
dedicated faculty for higher studies and professional career in various Scientific, Engineering and
Technological streams leading to proficiency in the field of Information Technology”
PROGRAM SPECIFIC OUTCOMES (PSOs)

PSO1: Able to apply knowledge of information management and communication systems to provide secured
solutions for real time engineering applications.
PSO2: Apply best software engineering practices, modern tools and technologies to deliver quality products.

PROGRAM OUTCOMES (Pos)


Engineering Graduates will be able to:
1. Engineering knowledge: Apply the knowledge of mathematics, science, engineering fundamentals, and an
engineering specialization to the solution of complex engineering problems.
2. Problem analysis: Identify, formulate, review research literature, and analyze complex engineering problems
reaching substantiated conclusions using first principles of mathematics, natural sciences, and engineering
sciences.
3. Design/development of solutions: Design solutions for complex engineering problems and design system
components or processes that meet the specified needs with appropriate consideration for the public health and
safety, and the cultural, societal, and environmental considerations.
4. Conduct investigations of complex problems: Use research-based knowledge and research methods
including design of experiments, analysis and interpretation of data, and synthesis of the information to provide
valid conclusions.
5. Modern tool usage: Create, select, and apply appropriate techniques, resources, and modern engineering and
IT tools including prediction and modeling to complex engineering activities with an understanding of the
limitations.
6. The engineer and society: Apply reasoning informed by the contextual knowledge to assess societal, health,
safety, legal and cultural issues and the consequent responsibilities relevant to the professional engineering
practice.
7. Environment and sustainability: Understand the impact of the professional engineering solutions in societal
and environmental contexts, and demonstrate the knowledge of, and need for sustainable development.
8. Ethics: Apply ethical principles and commit to professional ethics and responsibilities and norms of the
engineering practice.
9. Individual and team work: Function effectively as an individual, and as a member or leader in diverse
teams, and in multidisciplinary settings.
10. Communication: Communicate effectively on complex engineering activities with the engineering
community and with society at large, such as, being able to comprehend and write effective reports and design
documentation, make effective presentations, and give and receive clear instructions.
11. Project management and finance: Demonstrate knowledge and understanding of the engineering and
management principles and apply these to one’s own work, as a member and leader in a team, to manage projects
and in multidisciplinary environments.
12. Life-long learning: Recognize the need for, and have the preparation and ability to engage in independent
and life-long learning in the broadest context of technological change.

COURSE OUTCOMES

CO6: Demonstrate the OOP concepts, File I/O functions and Exception handling in C++.

Course Outcomes-Program Outcomes mapping

Program Outcomes Program Specific


COs Outcomes
PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12 PSO1 PSO2
CO-6 1 2 2 3 2 1 2

Rubrics for assessing student’s performance in Laboratory courses

The internals marks of lab for 2022 scheme (Odd) is 15 for Continuous Evaluation and 10 for Lab Internals
Continuous Evaluation for 2022 scheme:

Sl No Parameters Mark 5 4 3 0
1. Writing 5 The student is The student is The student has The student is
Program/Logic able to write able to write the written not attempted
(present the program program with incomplete to write
week’s/previous without any minor logical program with program.
week’s) logical and error major logical
syntactical and syntactical
error and error
proper
indentation is
followed.
Parameters 3 3 3 2 1

2. Implementation in 3 Student is able Student is able Student is The student


the target language to execute, to execute the executed the has not
with different inputs debug, and test program, but program executed the
the program for fails to debug, partially(fails to program.
all possible and test the meet desired
inputs/test program for all output)
cases. possible
inputs/test
cases.
Parameters 5 4 3 0
3. Record 5 Student Student submits Student fails to The student
submits the the record on submit the does not
record on time time but not record on time. submit the
and, neatly documented record.
documented properly with all
with all possible
possible input/output
input/output samples.
samples.
Parameters 4 4 2 1 0
4. Viva 2 Student Student answers Student Student fails
answers for at for at least 60% answers for at to answer any
least 80% of of questions least 40% of question
questions questions
5. Internal Assessment 10
Programming Assignments

SL. Name of Program Page


NO No
1 Write a C++ program to sort the elements in ascending and descending order.
9
2 Write a C++ program to find the sum of all the natural numbers from 1 to n. 11

3 Write a C++ program to swap 2 values by writing a function that uses call by 12
reference technique
Write a C++ program to demonstrate function overloading for the following
prototypes
4 13
add(int a, int b)
add(double a, double b)
Create a class named Shape with a function that prints "This is a shape". Create
another class named Polygon inheriting the Shape class with the same function
that prints "Polygon is a shape". Create two other classes named Rectangle and
Triangle having the same function which prints "Rectangle is a polygon" and
5 14
"Triangle is a polygon" respectively. Again, make another class named Square
having the same function which prints "Square is a rectangle”. Now, try calling the
function by the object of each of these classes.

Suppose we have three classes Vehicle, FourWheeler, and Car. The class
Vehicle is the base class, the class FourWheeler is derived from it and the
class Car is derived from the class FourWheeler. Class Vehicle has a method
'vehicle' that prints 'I am a vehicle', class FourWheeler has a method
'fourWheeler' that prints 'I have four wheels', and class Car has a method
'car' that prints 'I am a car'. So, as this is a multi-level inheritance; we can
6 16
have access to all the other classes’ methods from the object of the class Car.
We invoke all the methods from a Car object and print the corresponding
outputs of the methods. So, if we invoke the methods in this order, car (),
fourWheeler (), and vehicle (), then the output will be I am a car I have four
wheels I am a vehicle Write a C++ program to demonstrate multilevel
inheritance using this.
Write a C++ program to create a text file, check file created or not, if created
7 18
it will write some text into the file and then read the text from the file.
Write a C++ program to write and read time in/from binary file using
8 20
fstream
Write a function which throws a division by zero exception and catch it in
9 catch block. Write a C++ program to demonstrate usage of try, catch and 22
throw to handle exception.
Write a C++ program function which handles array of bounds exception
10 using C++. 23
Introduction to C++ Programming (BPLCK105D)
2023-24

` LABORATORY PROGRAMS

1. Write a C++ program to sort the elements in ascending and descending order.

#include<iostream.h>
//using namespace std;
int main ()
{
int num[20],n;
int i, j, temp;
cout<<"\n Enter the size of Array: \n";
cin>>n;
cout<<"Enter the Array value";
for (i = 0; i < n; ++i)
cin>>num[i];
for (i = 0; i < n; ++i) // 'for' loop is used for sorting the numbers in descending order
{
for (j = i + 1; j < n; ++j)
{
if (num[i] < num[j])
{
temp = num[i];
num[i] = num[j];
num[j] = temp;
}
}
}
cout<<"\n Numbers in Descending Order : \n";
for (i = 0; i < n; ++i)
{
cout<<" "; cout<<num[i]; cout<<"\n";
}
for(i=0;i<n;i++) //outer-loop for sorting the numbers in ascending order
{
for(int j=0;j<n;j++) //inner-loop
{
if(num[i]<num[j]) // represent second element in the array list
{
temp = num[i]; // first array element assign to variable temp

Information Science & Engineering,AIT Bangalore 9


Introduction to C++ Programming (BPLCK105D)
2023-24

num[i] = num[j]; // second element assigning to first element


num[j] = temp;

}
}
}
cout<<"\n Numbers in Ascending Order : \n";
for (i = 0; i<n; ++i)
{
cout<<" ";
cout<<num[i];
cout<<"\n";
}
}

Output:
Enter the size of Array:
4
Enter the Array value
6
3
7
1

Numbers in Descending Order:


7
6
3
1

Numbers in Ascending Order:


1
3
6
7

Information Science & Engineering,AIT Bangalore 10


Introduction to C++ Programming (BPLCK105D)
2023-24

2. Write a C++ program to find the sum of all the natural numbers from 1 to n.

#include<iostream.h>
//using namespace std;
int main()
{
int n;
cout << "Enter a number : ";
cin >> n;
int sum=0;
for(int i=1;i<=n;i++) sum+=i;
cout <<"Sum of " <<n <<" natural numbers is:\n"<< sum;
return 0;
}

Output:
Enter a number: 5
Sum of 5 natural numbers is:
15

Information Science & Engineering,AIT Bangalore 11


Introduction to C++ Programming (BPLCK105D)
2023-24

3. Write a C++ program to swap 2 values by writing a function that uses call by
reference technique.

#include<iostream.h>
//using namespace std;
void swap(int &,int &);
int main()
{
int x,y;
cout<<"\nEnter 1st number:: ";
cin>>x;
cout<<"\nEnter 2nd number:: ";
cin>>y;
cout<<"\n Before swapping the numbers are:"<<"\n\t x = "<<x<<"\n\t y = "<<y<<endl;
swap(x,y);
cout<<"\n After swapping the numbers are:"<<"\n\t x = "<<x<<"\n\t y = "<<y<<endl;
return 0;
}

void swap (int &num1, int &num2) //&num1 and &bnum2 are Reference variables
{
int temp;
temp=num1;
num1=num2;
num2=temp;
}

Output:

Enter 1st number :: 12

Enter 2nd number :: 23

Before swapping the numbers are:


x = 12
y = 23

After swapping the numbers are:


x = 23
y = 12

Information Science & Engineering,AIT Bangalore 12


Introduction to C++ Programming (BPLCK105D)
2023-24

4. Write a C++ program to demonstrate function overloading for the following


prototypes.
add(int a, int b)
add(double a, double b)

#include <iostream>
using namespace std;
// function with 2 integer parameters
void add(int a, int b)
{
int c;
cout<< "Integer number1: " << a<< endl;
cout<< " and Integer number2: " << b << endl;
c=a+b;
cout<<”Add of 2 numbers:”<<c;
}

// function with 2 double parameter

void add(double p, double q)


{
double d;
cout << "Double number1: " << p << endl;
cout << "Double number2: " << q << endl;
d=p+q;
cout<<”Addition of 2 double numbers”<<d;

}
int main()
{
int x = 5 , y=10;
double l = 5.5, m=10.5;

// call function with int parameter


add(x,y);

// call function with double parameters


add(l, m);
return 0;
}

Output:
Integer number 1: 5
and Integer number 2: 10
Double number1: 5.5
Double number2: 10.5

Information Science & Engineering,AIT Bangalore 13


Introduction to C++ Programming (BPLCK105D)
2023-24

5. Create a class named Shape with a function that prints "This is a shape". Create another
class named Polygon inheriting the Shape class with the same function that prints
"Polygon is a shape". Create two other classes named Rectangle and Triangle having the
same function which prints "Rectangle is a polygon" and "Triangle is a polygon"
respectively. Again, make another class named Square having the same function which
prints "Square is a rectangle”. Now, try calling the function by the object of each of these
classes.

#include <iostream>
#include <iomanip>
using namespace std;

class Shape
{
public:
void show();
};
void Shape::show()
{
cout << "This is a Shape" << endl;
}

class Polygon : public Shape


{
public:
void show();
};
void Polygon::show()
{
cout << "Polygon is a Shape" << endl;
}

class Triangle : public Polygon


{
public:
void show();
};
void Triangle::show()
{
cout << "Triangle is a Polygon" << endl;
}

class Rectangle : public Polygon


{
public:
void show();

Information Science & Engineering,AIT Bangalore 14


Introduction to C++ Programming (BPLCK105D)
2023-24

};
void Rectangle::show()
{
cout << "Rectangle is a Polygon" << endl;
}
class Square : public Rectangle
{
public:
void show();
};
void Square::show()
{
cout << "Square is a Rectangle" << endl;

}
int main()
{
Shape s1;
Polygon p1;
Rectangle r1;
Triangle t1;
Square sq1;

s1.show();
p1.show();
r1.show();
t1.show();
sq1.show();
return 0;
}

Output:

This is a Shape
Polygon is a Shape
Rectangle is a Polygon
Triangle is a Polygon
Square is a Rectangle

Information Science & Engineering,AIT Bangalore 15


Introduction to C++ Programming (BPLCK105D)
2023-24

6. Suppose we have three classes Vehicle, FourWheeler, and Car. The class Vehicle is the
base class, the class FourWheeler is derived from it and the class Car is derived from the
class FourWheeler. Class Vehicle has a method 'vehicle' that prints 'I am a vehicle', class
FourWheeler has a method 'fourWheeler' that prints 'I have four wheels', and class Car
has a method 'car' that prints 'I am a car'. So, as this is a multi-level inheritance; we can
have access to all the other classes methods from the object of the class Car. We invoke all
the methods from a Car object and print the corresponding outputs of the methods.
So, if we invoke the methods in this order, car(), fourWheeler(), and vehicle(), then the
output will be
I am a car
I have four wheels I am a vehicle
Write a C++ program to demonstrate multilevel inheritance using this.

#include<iostream>
#include<iomanip> using
namespace std;

class Vehicle
{
public:
void vehicle();
};

void Vehicle::vehicle()
{
cout << "I am a vehicle" << endl;

class FourWheeler : public Vehicle


{
public:
void fourWheeler();

};
void FourWheeler::fourWheeler(){
cout << "I have four wheels" << endl;

Information Science & Engineering,AIT Bangalore 16


Introduction to C++ Programming (BPLCK105D)
2023-24

class Car : public FourWheeler


{
public:
void car();

};
void Car::car(){
cout << "I am a car" << endl;

}
int main()

Car myCar;
myCar.car();
myCar.fourWheeler();
myCar.vehicle();
return 0;
}

Output:
I am a car
I have four wheels
I am a vehicle

Information Science & Engineering,AIT Bangalore 17


Introduction to C++ Programming (BPLCK105D)
2023-24

7. Write a C++ program to create a text file, check file created or not, if created it will write
some text into the file and then read the text from the file

#include <iostream>
#include <fstream>
using namespace std;
int main()
{
string flName;
char mesg[40], ch;

cout << "Enter the file name you want to create : ";
cin >> flName;
cin.get(); //read the trailing enter character

ofstream fout(flName.c_str());
// fout.close();
if(fout.fail())
{
cout << "\nFailed to create file." << endl;

}
else
{
cout << "\nFile " << flName <<" created successfully" << endl;

cout << "Enter a message : ";


cin.getline(mesg,40);
fout << mesg << endl;
cout << "\nMessage written to file successfully\n" << endl;
fout.close();

ifstream fin(flName.c_str());
cout << "Here are the contents of " << flName << ":\n";

while (fin.get(ch))
{
cout << ch; // write it to screen
cout << "\nDone reading file contents\n" << endl;
}
fin.close();
return 0;

Information Science & Engineering,AIT Bangalore 18


Introduction to C++ Programming (BPLCK105D)
2023-24

Output:
Enter the file name you want to create : hello.txt

File hello.txt created successfully


Enter a message : Hello Acharya

Message written to file successfully

Here are the contents of hello.txt:


Hello Acharya

Done reading file contents

Information Science & Engineering,AIT Bangalore 19


Introduction to C++ Programming (BPLCK105D)
2023-24

8. Write a C++ program to write and read time in/from binary file using fstream

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstring>
using namespace std;
class timeVal
{
int hh, mm, ss; char ampm[3];
public:
void setdata(int h, int m, int s, const char* half)
{
hh = h; mm = m; ss = s;
strcpy(ampm, half);
}
void showdata()
{
cout << "\nThe Time is : ";
cout << setfill('0') << setw(2) << hh << ":";
cout << setfill('0') << setw(2) << mm << ":";
cout << setfill('0') << setw(2) << ss << " ";
cout << ampm << endl << endl;
}
};
int main()
{

timeVal writeObj, readObj;


int hh, mm, ss;
char ampm[3];
cout << "Enter Hours : ";
cin >> hh;
cout << "Enter Minutes : ";
cin >> mm;
cout << "Enter Seconds : ";
cin >> ss;
cout << "Enter am or pm : ";
cin >> ampm;
writeObj.setdata(hh,mm,ss,ampm);
ofstream outFile("TimeFile", ios::out | ios::binary);
if(!outFile)
{
cout << "Cannot open file.\n";
return 1;
}

Information Science & Engineering,AIT Bangalore 20


Introduction to C++ Programming (BPLCK105D)
2023-24

outFile.write((char *) &writeObj,sizeof(timeVal));
cout << "\nWritten the time object successfully to binary file" << endl;
outFile.close();

// now, read back;


ifstream inFile("TimeFile", ios::in | ios::binary);
if(!inFile)
{
cout << "Cannot open file.\n";
return 1;
}
inFile.read((char *) &readObj, sizeof(timeVal));
cout << "\nRead the time object successfully from binary file" << endl; readObj.showdata();
inFile.close();
return 0;
}

Output:
Enter Hours : 3
Enter Minutes : 4
Enter Seconds : 5
Enter am or pm : am

Written the time object successfully to binary file

Read the time object successfully from binary file

The Time is : 03:04:05 am

Information Science & Engineering,AIT Bangalore 21


Introduction to C++ Programming (BPLCK105D)
2023-24

9. Write a function which throws a division by zero exception and catch it in catch block.
Write a C++ program to demonstrate usage of try, catch and throw to handle exception.

#include <iostream>
using namespace std;
int fdivide(int,int);
int main()
{
int numer,denom,res;
cout<<"Enter the value of numerator and denominator\n";
cin>>numer>>denom;
res=fdivide(numer,denom);
return res;
}

int fdivide(int n1,int n2)

{
try
{
int a,b,c;
a=n1;
b=n2;
c=a/b;
if (b==0)
{
throw b;
}
cout<<"Result="<<c;
}
catch (int b)
{
cout<<"Cannot divide by zero"<<b;
}
}

Output:

Enter the value of m and n : 8


2
8 divided by 2 is equal to 4

Information Science & Engineering,AIT Bangalore 22


Introduction to C++ Programming (BPLCK105D)
2023-24

Enter the value of m and n : 4


0

Division by Zero Exception occurred.

10. Write a C++ program function which handles array of bounds exception using C++.

#include <iostream>
using namespace std;
int read_array(int[],int );
int disp_array(int[],int);
int main()
{
int arr[6],n,i;
cout<<"Enter the number of elements to consider\n";
cin>>n;
try
{
if(n>6)
{
throw "Array out of Bound Exception error\n";
}
read_array(arr,n);
disp_array(arr,n);
}
catch(const char *msg)
{
cout<<msg<<" Pls Enter the number of elements less than or equal to "<<6<<"\n";
}
return 0;
}

int read_array(int a[],int n)


{
int i;
cout<<"Enter the elements of array\n";
for(i=0;i<n;++i)
{
cin>>a[i];
}

Information Science & Engineering,AIT Bangalore 23


Introduction to C++ Programming (BPLCK105D)
2023-24

return 0;
}

int disp_array(int a[],int n)


{
int i;
cout <<"The contents of array are\n";
for(i=0;i<n;i++)
{
cout<<a[i]<<"\t";
}
cout<<"\n";
return 0;
}
Output 1:
Enter the number of elements to consider
5
Enter the elements of array
3
4
2
5
7
The contents of array are
3 4 2 5 7

Output 2:
Enter the number of elements to consider
7
Array out of Bound Exception error

Please Enter the number of element

Information Science & Engineering,AIT Bangalore 24

You might also like