0% found this document useful (0 votes)
29 views

BCSL 032 Solved Assignment

This document contains solutions to C++ programming assignments for a BCA course. It includes programs to: 1) Display the table of a given number using formatting. 2) Demonstrate the use of ::, ?:, and sizeof() operators. 3) Create a Book class with default and parameterized constructors and display book details. 4) Overload the + operator to concatenate two strings. 5) Handle exceptions by checking if student marks entered are between 0-100.

Uploaded by

learnerlexi
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)
29 views

BCSL 032 Solved Assignment

This document contains solutions to C++ programming assignments for a BCA course. It includes programs to: 1) Display the table of a given number using formatting. 2) Demonstrate the use of ::, ?:, and sizeof() operators. 3) Create a Book class with default and parameterized constructors and display book details. 4) Overload the + operator to concatenate two strings. 5) Handle exceptions by checking if student marks entered are between 0-100.

Uploaded by

learnerlexi
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/ 8

www.techiya.

in

www.techiya.in
BCA Solved Assignment 2020-21

Course Code : BCSL-032


Course Title : C++ Programming Lab
Assignment Number: BCA (3)/BCSL-032/Assignment/2020-21

Q1. a) Write a C++ program to display the table of a given number. Use
appropriate formatting todisplay the table.
Ans:
#include <iostream.h>
int main()
{
int n;
cout<< "Enter a positive integer: ";
cin>> n;
for (inti = 1; i<= 10; ++i) {
cout<< n << " * " <<i<< " = " << n * i<<endl;
}
return 0;
}
Output:

1
www.techiya.in

Q1. (b) Write C++ program to demonstrate use of followings:


(i) ::
(ii) ?:
(iii) sizeof( )
Ans:
#include<iostream.h>
classNumberCheck
{
public:
voidOddEvenCheck(int n);
};

/* To define a Function outside of a Class using :: (Scope Resolution Operator) */


voidNumberCheck::OddEvenCheck(int n)
{
cout<<endl;
2
www.techiya.in

/* Determine Odd or Even using ?: (Ternary Operator) */


(n%2 == 0)? cout<< "Even Number" : cout<< "Odd Number";
}
int main()
{
int number;
cout<< "Enter the Number : ";
cin>> number;
NumberCheck ob1;
ob1.OddEvenCheck(number);
/* Returns the size, in bytes, of the given expression or type */
cout<<endl<<sizeof(number);
return 0;
}

Output:

Q1. (c) Write a C++ program to create class named Book. Define appropriate
constructors (using concept of overloading) to create differ objects. Define methods for
3
www.techiya.in

displaying book details including book’s author name, publisher, price of book. Make
necessary assumptions wherever required.
Ans:
#include<iostream.h>
#include<stdio.h>
#include<string.h>
class Book
{
private:
char book_title[100];
char book_author[50];
char book_publisher[100];
float book_price;

public:
/* Default Constructor */
Book()
{
strcpy(book_title, "-BLANK-");
strcpy(book_author, "-BLANK-");
strcpy(book_publisher, "-BLANK-");
book_price = 0.0;
}

/* Parameterized Constructor */
Book(char title[], char author[], char publisher[], float price)
{
strcpy(book_title, title);
strcpy(book_author, author);
strcpy(book_publisher, publisher);
book_price = price;
}

/* Display Book Details */


void Display()
{
cout<<endl<< "TITLE - " <<book_title;
cout<<endl<< "AUTHOR - " <<book_author;
cout<<endl<< "PUBLISHER - " <<book_publisher;
cout<<endl<< "PRICE - " <<book_price;
4
www.techiya.in

}
};
int main()
{
char b_title[100], b_author[50], b_publisher[100];
float b_price;
cout<< "Enter the Book Title : ";
gets(b_title);
cout<< "Enter the Author Name : ";
gets(b_author);
cout<< "Enter the Publisher Name : ";
gets(b_publisher);
cout<< "Enter the Book Price : ";
cin>>b_price;
/* Create Object using Defailt Constructor */
Book ob1;
/* Create Object using Parameterized Constructor */
Book ob2(b_title, b_author, b_publisher, b_price);
ob2.Display();
return 0;
}
Output:

5
www.techiya.in

Q2. (a) Write C++ program for concatenation of two strings using ‘+’ operator
overloading. Makenecessary assumptions wherever required.
Ans:
#include <iostream.h>
#include <string.h>
// Class to implement operator overloading
// function for concatenating the strings
classAddString {
public:
// Classes object of string
char s1[25], s2[25];
// Parametrized Constructor
AddString(char str1[], char str2[])
{
// Initialize the string to class object
strcpy(this->s1, str1);
strcpy(this->s2, str2);
}
// Overload Operator+ to concat the string
void operator+()
{
cout<< "\nConcatenation: " <<strcat(s1, s2);
}
};
// Driver Code
int main()
{
// Declaring two strings
char str1[] = "Geeks";
char str2[] = "ForGeeks";
// Declaring and initializing the class
// with above two strings
AddStringa1(str1, str2);
// Call operator function
+a1;
return 0;
}

6
www.techiya.in

Output:

Q2. (b) Write a C++ program to demonstrate exception handling. This program
takes marks of tenstudents in a subject as input and store it in an array. Make
provisions so that if marks entered asinput is less than 0 or greater than 100 then
message “Invalid Input” is displayed and programterminate normally.
Ans:
#include<iostream>
using namespace std;
int main ()
{
int array[10], i;
try
{
for(i=0;i<10;i++)
{
cout<< "Enter the Marks : ";
7
www.techiya.in

cin>> array[i];
if( array[i] < 0 || array[i] > 100 )
{
throw "Invalid Input !";
}
}
}
catch(const char* error)
{
cerr<<endl<< error;
}
return 0;
}
Output:

You might also like