below are practicals and their solutions for Mumbai University FYBsc computer science 2023 updated complete list.
Aim : Write a program to demonstrate the use of data members and member functions.
Code:
#include<iostream>
using namespace std;
class person {
public:
string name;
int number;
void read() {
cout << “Enter the name:”;
getline(cin,name);
cout << “Enter the number:”;
cin >> number;
}
void print() {
cout << “name:” << name << ” number:” << number;
}
};
int main() {
person obj;
cout << “Simple class and object example” << endl;
obj.read();
obj.print();
return 0;
}
Output :
AIM : Write a Program based on branching and looping statements using classes
Code:
#include<iostream>
using namespace std;
int main() {
int i, ch;
char b[100];
for(i=0; i<5; i++) {
cout << “Enter the choice: 1 – computer 2 – keyboard \n”;
cin >> ch;
switch(ch) {
case 1: {
cout << “You have choosen computer\n”;
break;
}
case 2: {
cout << “You have choosen keyboard\n”;
break;
}
}
}
}
Output :
Aim : Write a Program to demonstrate one and two dimensional array using classes :-
a) One Dimension Array :
Code :
#include<iostream>
using namespace std;
int main()
{
int abc[5], i;
for (i=0; i<=4; i++)
{
cout << “Enter value in element” << i << endl;
cin >> abc[i];
}
cout << “output in reverse order” << endl;
for (i=4; i>=0; i–)
{
cout << “value in a[“<<i<<“] “<<abc[i]<<endl;
}
}
Output :
b) Multi Dimension Array :
Code :
#include<iostream>
using namespace std;
int main()
{
int mat[3][3]={2,1,4,3,2,7,5,6,8};
int r,c,sum=0;
for(r=0; r<3; r++)
{
for(c=0; c<3; c++){
cout<<mat[r][c]<<“\t”;
cout<<endl;
}
}
for(r=0; r<3; r++){
for(c=0; c<3; c++){
sum += mat[r][c];
cout<<sum<<” “;
}
cout << endl;
}
return 0;
}
Output :
Aim : Write a Program to use scope resolution operator. Display the various values of the same variables declared at different scope levels.
Code :
#include <iostream>
using namespace std;
int my_variable = 10;
int main()
{
int my_variable = 100;
cout << “Value of global my_variable is ” << ::my_variable << endl;
cout << “Value of local my_variable is ” << my_variable << endl;
return 0;
}
Output :
Aim : Write a Program to demonstrate various types of constructor and destructors.
Default constructor :
Code :
#include<iostream>
using namespace std;
class A
{
// constructor default
A()
{
cout << “Constructor called”;
}
// destructor default
~A()
{
cout << “Destructor called”;
}
};
int main()
{
A obj1; // Constructor
int x = 1;
if(x)
{
A obj2; // Constructor
} // Destructor obj2
} // Destructor obj1
Output :
Parameterized constructor :
Code :
#include<iostream>
using namespace std;
class Wall {
private:
double length;
double height;
public:
Wall(double len, double hgt) {
length = len;
height = hgt;
}
~Wall(){
cout << “Destructor called.” << endl;
}
double calculateArea() {
return length * height;
}
};
int main() {
Wall wall1(9.5, 11.5);
cout << “Area of first wall is: ” << wall1.calculateArea() << endl;
return 0;
}
Output :
Aim : Write a Program to demonstrate use of public, protected and private scope specifier:
a) Protected Scope Specifier:
Code :
#include <iostream>
using namespace std;
class Parent {
protected:
int id_protected;
};
class Child : public Parent {
public:
void setId(int id){
id_protected = id;
}
void displayId(){
cout << “id_protected is: ” << id_protected << endl;
}
};
int main(){
Child obj1;
obj1.setId(81); obj1.displayId();
return 0;
}
Output :
b) Private Scope Specifier:
Code :
#include<iostream>
using namespace std;
class Circle {
private:
double r;
public:
void calculate_area(double radius){
r = radius;
double area = 3.14 * r * r;
cout << “Area is ” << area;
}
};
int main() {
Circle c1;
c1.calculate_area(15);
}
Output :
c) Public Scope Specifier:
Code :
#include <iostream>
using namespace std;
class Circle {
public:
double radius;
double calculate_area(){
return 3.14 * radius * radius;
}
};
int main(){
Circle c1;
c1.radius = 14.5;
cout << “Radius is: ” << c1.radius << endl;
cout << “Area is: ” << c1.calculate_area();
return 0;
}
Output :
Aim : Program to demonstrate single and multilevel inheritance
a) Single inheritance:
#include<iostream>
using namespace std;
class Vehicle {
public:
Vehicle(){
cout << “This is Vehical \n”;
}
};
class Car : public Vehicle {
};
int main() {
Car obj;
return 0;
}
Output :
b) Multilevel inheritance :
Code :
#include<iostream>
using namespace std;
class Vehicle {
public:
Vehicle() {
cout << “This is a Vehicle \n”;
}
};
class fourWheeler : public Vehicle {
public:
fourWheeler(){
cout << “Objects with 4 wheels are vehicles\n”;
}
};
class Car : public fourWheeler {
public:
Car(){
cout << “Car has 4 Wheels \n”;
}
};
int main() {
Car obj;
return 0;
}
Output :
Aim : Write a Program to demonstrate multiple inheritance and hierarchical inheritance
a) Multiple inheritance:
Code :
#include <iostream>
using namespace std;
class A
{
public:
int x;
void getx()
{
cout << “enter value of x: “; cin >> x;
}
};
class B
{
public:
int y;
void gety()
{
cout << “enter value of y: “; cin >> y;
}
};
class C : public A, public B //C is derived
{
public:
void sum()
{
cout << “Sum = ” << x + y;
}
};
int main()
{
C obj1;
obj1.getx();
obj1.gety();
obj1.sum();
return 0;
}
Output :
b) Hierarchical inheritance:
Code :
#include <iostream>
using namespace std;
class university
{
private:
string university_name;
public:
string getUniversityName()
{
university_name = “mumbai university”;
return university_name;
}
};
class student: public university
{
public:
void display_student()
{
cout << “I am a student of the ” << getUniversityName() << “.\n\n”;
}
};
class faculty: public university
{
public:
void display_faculty()
{
cout << “I am a professor in the ” << getUniversityName() << “.\n\n”;
}
};
int main()
{
student obj1;
obj1.display_student();
faculty obj2;
obj2.display_faculty();
return 0;
}
Output :
Aim : Write a Program to demonstrate inheritance and derived class constructors.
Code :
#include<iostream>
using namespace std;
class baseClass
{
public:
baseClass()
{
cout << “I am baseClass constructor” << endl;
}
~baseClass()
{
cout << “I am baseClass destructor” << endl;
}
};
class derivedClass: public baseClass
{
public:
derivedClass()
{
cout << “I am derivedClass constructor” << endl;
}
~derivedClass()
{
cout <<” I am derivedClass destructor” << endl;
}
};
int main()
{
derivedClass D;
return 0;
}
Output :
Aim : Write a Program to demonstrate friend function, inline function and this pointer.
a) friend function:
Code :
#include <iostream>
using namespace std;
class Distance {
private:
int meter;
friend int addFive(Distance);
public:
Distance() : meter(0) {}
};
int addFive(Distance d) {
d.meter += 5;
return d.meter;
}
int main() {
Distance D;
cout << “Distance: ” << addFive(D);
return 0;
}
Output :
b) Inline function :
Code :
#include<iostream>
using namespace std;
inline int maxNumber(int x, int y) {
return ( x > y) ? x : y;
}
int main() {
cout << “Max of 0 and 20 is ” << maxNumber(0, 20) << endl;
cout << “Max number of 7 and 200 is ” << maxNumber(7, 200) << endl;
cout << “Max number of 222 and 444 is ” << maxNumber(222,444);
}
Output :
c) this pointer :
Code :
#include<iostream>
using namespace std;
class Employee {
public:
int id;
string name;
float salary;
Employee(int id, string name, float salary) {
this -> id = id;
this -> name = name;
this -> salary = salary;
}
void display() {
cout<< “The ID is ” << id << endl;
cout << “The Name is ” << name << endl;
cout << “The Salary is ” << salary << “\n”;
}
};
int main(void) {
Employee e1 = Employee(01, “ravish”, 20000);
Employee e2 = Employee(02, “kiran”, 15000);
e1.display();
e2.display();
return 0;
}
Output :
Aim : Write a Program to function overloading and overriding.
a) Function overloading :
Code :
#include<iostream>
using namespace std;
//functions
void test(int);
void test(float);
void test(int, float);
int main() {
int a = 5;
float b = 5.6;
test(a);
test(b);
test(a,b);
}
void test(int a) {
cout << “Integer a is ” << a << endl;
}
void test(float b) {
cout << “Float b is ” << b << endl;
}
void test(int a, float b) {
cout << “Integer a and Float b is ” << a << b;
}
Output :
b) Function overriding :
Code :
#include <iostream>
using namespace std;
class Base {
public:
void print() {
cout << “Base Function” << endl;
}
};
class Derived : public Base {
public:
void print() {
cout << “Derived Function” << endl;
}
};
int main() {
Derived derived1, derived2;
derived1.print();
// base class
derived2.Base::print();
return 0;
}
Output :
Aim : Write a Program to demonstrate pointers.
Code :
#include <iostream>
using namespace std;
int main()
{
int number=30;
int *p;
p = &number;//address of number
cout << “Address of number variable is:” << &number << endl;
cout << “Address of p variable is:” << p << endl;
cout << “Value of p variable is:”<< *p << endl;
return 0;
}
Output :
Aim : Write a Programs to demonstrate text and binary file handling.
a) Text file :
Code :
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream fio;
string line;
// file.txt
fio.open(“file.txt”, ios::trunc | ios::out | ios::in);
while (fio) {
getline(cin, line);
if (line == “-1”)
break;
fio << line << endl;
}
fio.seekg(0, ios::beg);
while (fio) {
getline(fio, line);
cout << line << endl;
}
fio.close();
return 0;
}
Output :
file.txt
b) Binary file :
Code :
#include<iostream>
#include<fstream>
using namespace std;
struct Student {
int roll_no;
string name;
};
int main() {
ofstream wf(“student.dat”, ios::out | ios::binary);
if(!wf) {
cout << “Cannot open file!” << endl;
return 1;
}
Student wstu[3];
wstu[0].roll_no = 1;
wstu[0].name = “Ram”;
wstu[1].roll_no = 2;
wstu[1].name = “Shyam”;
wstu[2].roll_no = 3;
wstu[2].name = “Madhu”;
for(int i = 0; i < 3; i++)
wf.write((char *) &wstu[i], sizeof(Student));
wf.close();
if(!wf.good()) {
cout << “Error occurred at writing time!” << endl;
return 1;
}
ifstream rf(“student.dat”, ios::out | ios::binary);
if(!rf) {
cout << “Cannot open file!” << endl;
return 1;
}
Student rstu[3];
for(int i = 0; i < 3; i++)
rf.read((char *) &rstu[i], sizeof(Student));
rf.close();
if(!rf.good()) {
cout << “Error occurred at reading time!” << endl;
return 1;
}
cout<<“Student’s Details:”<<endl;
for(int i=0; i < 3; i++) {
cout << “Roll No: ” << wstu[i].roll_no << endl;
cout << “Name: ” << wstu[i].name << endl;
cout << endl;
}
return 0;
}
Output :