Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 107

PROGRAMMING IN C++ MCA 1st SEMESTER

1. Write a c++ program to demonstrate the use of cout, cin ,and endl.
Program :-
Cout –
#include<iostream>
using namespace std;
int main()
{
char arr[]= "welcome to c++ tutorial";
cout<<"value of arr is:"<<arr<<endl;
}
Output :-

Cin :-
#include<iostream>
using namespace std;
int main()
{
int age;
cout<<"enter your age:";
cin>>age;
cout<<"your age is"<<age<<endl;
}
Output :-

NITIN KUMAR SINGAUR 1|Page


PROGRAMMING IN C++ MCA 1st SEMESTER

Endl-

#include<iostream>
using namespace std;
int main()
{
int age;
cout<<"c++ tutorial:";
cout<<"books"<<endl;
cout<<"end of line"<<endl;
}
Output :-

NITIN KUMAR SINGAUR 2|Page


PROGRAMMING IN C++ MCA 1st SEMESTER

2. Write a c++ program to demonstrate arithmetic operator.


Program :-
#include<iostream>
using namespace std ;
int main()
{
int a=20;
int b=10;
int c;
cout<<"a+b is:"<<a+b<<endl;
cout<<"a-b is:"<<a-b<<endl;
cout<<"a*b is:"<<a*b<<endl;
cout<<"a/b is:"<<a/b<<endl;
cout<<"a%b is:"<<a%b<<endl;
return 0;
}
Output :-

NITIN KUMAR SINGAUR 3|Page


PROGRAMMING IN C++ MCA 1st SEMESTER

3. Write a c++ program to demonstrate minus unary operator.


Program :-
#include<iostream>
using namespace std;
int main()
{
int x = 10;
int y = -x;
cout<<"The value of x is:-"<<x<<endl;
cout<<"The value of y is:-"<<y<<endl;
return 0;
}
Output :-

NITIN KUMAR SINGAUR 4|Page


PROGRAMMING IN C++ MCA 1st SEMESTER

4. Write a c++ program to demonstrate to ternary operator.


Program :-
#include <iostream>
#include <string>
using namespace std;
int main() {
double marks;
// take input from users
cout << "Enter your marks: ";
cin >> marks;
// ternary operator checks if
// marks is greater than 40
string result = (marks >= 40) ? "passed" : "failed";
cout << "You " << result << " the exam.";
return 0;
}
Output :-

NITIN KUMAR SINGAUR 5|Page


PROGRAMMING IN C++ MCA 1st SEMESTER

5. Write a c++ program to cheak while number is prime or not.


Program :-
#include<iostream>
using namespace std;
int main()
{
int num,i,chk=0;
cout<<"Enter a number:";
cin>>num;
for(i=2;i<num;i++)
{
if(num%i==0)
{
chk++;
break;
}
}
if(chk==0)
cout<<"\nIt is a prime number";
else
cout<<"\nIt is not a prime number";
cout<<endl;
return 0;
}
Output :-

NITIN KUMAR SINGAUR 6|Page


PROGRAMMING IN C++ MCA 1st SEMESTER

6. Write a c++ program to implement local and global variable.


Program :-
Local variable -
#include <iostream>
using namespace std;
int main () {
// Local variable declaration:
int a, b;
int c;
// actual initialization
a = 10;
b = 20;
c = a + b;
cout << c;
return 0;
}
Output :-

Global variable –

#include<iostream>
using namespace std;
int a=10;
int main()
{
int a,b,c;
b=5;
c=6;

NITIN KUMAR SINGAUR 7|Page


PROGRAMMING IN C++ MCA 1st SEMESTER

a=b+c;
cout<<"a is"<<a<<endl;
cout<<"global variable is:"<<::a;
return 0;
}
Output :-

NITIN KUMAR SINGAUR 8|Page


PROGRAMMING IN C++ MCA 1st SEMESTER

7. Write a c++ program to implement symobolic constant using const


keyword.
Program :-
#include <iostream>
using namespace std;
int main()
{
const int y = 10;
cout << y;
return 0;
}
Output :-

NITIN KUMAR SINGAUR 9|Page


PROGRAMMING IN C++ MCA 1st SEMESTER

8. Write a c++ program to find factorial of given number.


Program :-
#include<iostream>
using namespace std;
int main()
{
int n =5, fact =1 ,i;
for(i=1; i<=n;i++)
fact =fact*i;
cout<<"factorial of "<<n<<" is"<<fact;
return 0;
}
Output :-

NITIN KUMAR SINGAUR 10 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

9. Write a c++ program to find given year is leap or not.


Program :-
#include <iostream>
using namespace std;
int main() {
int year;
cout << "Enter a year: ";
cin >> year;
if (year % 400 == 0) {
cout << year << " is a leap year.";
}
else if (year % 100 == 0) {
cout << year << " is not a leap year.";
}
else if (year % 4 == 0) {
cout << year << " is a leap year.";
}
else {
cout << year << " is not a leap year.";
}
return 0;
}
Output :-

NITIN KUMAR SINGAUR 11 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

10.Write a c++ program to demonstrate goto statement.


Program :-

#include <iostream>
using namespace std;
int main()
{
int num;
cout<<"Enter a number: ";
cin>>num;
if (num % 2==0)
{
goto print;
}
else
{
cout<<"Odd Number";
}
print:
cout<<"Even Number";
return 0;
}
Output :-

NITIN KUMAR SINGAUR 12 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

11.Write a c++ program to demonstrate break statement.


Program :-
#include<iostream>
using namespace std;
int main()
{
for(int i=1;i<=10;i++)
{
if(i==5)
{
break;
}
cout<<i<<"\n";
}
}

Output :-

NITIN KUMAR SINGAUR 13 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

12.Write a c++ program to demonstrate continue statement.


Program :-
#include<iostream>
using namespace std;
int main()
{
for(int i=1; i<=10;i++)
{
if(i==5)
{
continue;
}
cout<<i<<"\n";
}
}
Ouutput :-

NITIN KUMAR SINGAUR 14 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

13.Write a c++ program to build simple calculator( +, - , * , / ) using


switch statement.
Program :-
#include<iostream>
using namespace std;
int main()
{
char op;
float num1,num2;
cout<<"Enter a operator: +,-,*,/:";
cin>>op;
cout<<"enter two operands:";
cin>>num1>>num2;
switch(op)
{
case'+':
cout<<num1<<"+"<<num2<<"="<<num1+num2;
break;
case'-':
cout<<num1<<"-"<<num2<<"="<<num1-num2;
break;
case'*':
cout<<num1<<"*"<<num2<<"="<<num1*num2;
break;
case'/':
cout<<num1<<"/"<<num2<<"="<<num1/num2;
break;
default:
cout<<"error! operation is not correct";
break;
}

NITIN KUMAR SINGAUR 15 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

return 0;
}
Output :-

NITIN KUMAR SINGAUR 16 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

14.Write a c++ program to find largest number among three numbers.


Program :-
#include<iostream>
using namespace std;
int main()
{
float n1,n2,n3;
cout<<"Enter three number:";
cin>>n1>>n2>>n3;
if(n1>=n2&&n1>=n3)
cout<<"largest number:"<<n1;
if(n2>=n3&&n2>=n1)
cout<<"largest number:"<<n2;
if(n3>=n2&&n3>=n1)
cout<<"largest number:"<<n3;
return 0;
}
Output :-

NITIN KUMAR SINGAUR 17 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

15. Write a C++ program to take input of student details and display all the details using
structure.
Program :-

#include<iostream>
using namespace std;
struct student
{
char sname[20];
int rollno;
float fee;
};
int main()
{
struct student st;
cout<<"\n Enter student detail"<<"";
cout<<"\n---------------------\n";
cout<<"Student name:";
gets(st.sname);
cout<<"Roll Number:";
cin>>st.rollno;
cout<<"Student Fee:";
cin>>st.fee;

cout<<"\n\n Student Detail\n"<<"";


cout<<"--------------------";
cout<<"\nStudent Name:"<<st.sname;
cout<<"\nRoll NUmber:"<<st.rollno;
cout<<"\nStudent Fee:"<<st.fee;
return 0;
}

NITIN KUMAR SINGAUR 18 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

Output :-

16. Write a C++ program to demonstrate enum.

NITIN KUMAR SINGAUR 19 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

Program :-
#include<iostream>
using namespace std;
enum week{Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday};
int main()
{
week day;
day= Monday;
cout<<"Day:"<<day+1<<endl;
return 0;
}

Output :-

17.Write a C++ program to multiply given three variables using


function named “MUL”.

NITIN KUMAR SINGAUR 20 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

Program :-
#include<iostream>
using namespace std;
int main()
{
int num1 , num2 , num3, mul;
cout<<"Enter three variable:";
cin>>num1>>num2>>num3;
mul=num1*num2*num3;
cout<<"mul="<<mul;
return 0;

}
Output :-

18.Write a C++ program to demonstrate call by value.


Program :-

NITIN KUMAR SINGAUR 21 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

#include <iostream>
using namespace std;
void swap(int a, int b)
{
int temp = a;
a = b;
b = temp;
}
int main()
{
int x =4, y=5;
cout<<"The value of x is "<<x<<" and the value of y is "<<y<<endl;
swap(x, y);
cout<<"The value of x is "<<x<<" and the value of y is "<<y<<endl;
return 0;
}
Output :-

19.Write a C++ program to demonstrate call by reference.


Program :-
#include <iostream>

NITIN KUMAR SINGAUR 22 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

using namespace std;


void swapReferenceVar(int &a, int &b)
{
int temp = a;
a = b;
b = temp;
}
int main()
{
int x =4, y=5;
cout<<"The value of x is "<<x<<" and the value of y is "<<y<<endl;
swapReferenceVar(x, y);
cout<<"The value of x is "<<x<<" and the value of y is "<<y<<endl;
return 0;
}
Output :-

20.Write a C++ program to demonstrate passing argument to a function


as reference variable as argument.
Program :-
#include<iostream>

NITIN KUMAR SINGAUR 23 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

using namespace std;


void fun(int &x, int &y)
{
x = x+1;
y = y+1;
}
int main()
{
int a=2 , b=3;
cout<<"the value of a is "<<a<<" and the value of b is " <<b<<endl;
fun(a,b);
cout<<"the value of a is "<<a<<" and the value of b is " <<b<<endl;
return 0;
}

Output :-

21. Write a C++ program to demonstrate passing argument to a function


as structure variable as argument.
Program :-
#include<iostream>
using namespace std;

NITIN KUMAR SINGAUR 24 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

struct student
{
char name[10];
int rollno;
}s;
void display(student s)
{
cout<<"enter name";
cin>>s.name;
cout<<"enter rollno";
cin>>s.rollno;
cout<<s.name<<endl;
cout<<s.rollno;
}
int main()
{
display (s);
}

Output :-

22. Write a C++ program to demonstrate default argument.


Program :-
#include <iostream>
using namespace std;

NITIN KUMAR SINGAUR 25 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

int sum(int x, int y, int z = 0, int w = 0)


{
return (x + y + z + w);
}
int main()
{
// Statement 1
cout << sum(10, 15) << endl;

// Statement 2
cout << sum(10, 15, 25) << endl;

// Statement 3
cout << sum(10, 15, 25, 30) << endl;
return 0;
}
Output :-

23. Write a C++ program using inline function to calculate area of circle.
Program :-
#include<iostream>
using namespace std;

NITIN KUMAR SINGAUR 26 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

inline float Area(float radius)


{
return 3.1415 * radius * radius;
}
int main()
{
float radius;
cout<<"Enter Radius of circle=";
cin>>radius;
cout<<"Area of circle using Inline Function is : "<<Area(radius);
return 0;
}
Output :-

24. Write a C++ program to demonstrate static and dynamic


initialization of variable.
Program :-
#include <iostream>
using namespace std;

NITIN KUMAR SINGAUR 27 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

int main()
{
int a = 20;
int b;
cout << "The value of variable a : "<< a; // static initialization
cout << "\nEnter the value of variable b : "; // dynamic initialization
cin >> b;
cout << "\nThe value of variable b : "<< b;
return 0;
}
Output :-

25. Writea C++ program using function template to add two integers
and two float number.
Program :-
#include <iostream>

NITIN KUMAR SINGAUR 28 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

using namespace std;


template<class t1,class t2>
void sum(t1 a,t2 b) // defining template function
{
cout<<"\nSum="<<a+b<<endl;
}
int main()
{
int a,b;
float x,y;
cout<<"\nEnter two integer data: ";
cin>>a>>b;
cout<<"\nEnter two float data: ";
cin>>x>>y;
sum(a,b); // adding two integer type data
sum(x,y); // adding two float type data
return 0;
}
Output:-

26. Write a C++ program to demonstrate class templates.


Program :-
#include <iostream>
using namespace std;

NITIN KUMAR SINGAUR 29 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

template<class T>
class A
{
public:
T num1 = 5;
T num2 = 6;
void add()
{
std::cout << "Addition of num1 and num2 : " << num1+num2<<std::endl;
}
};
int main()
{
A<int> d;
d.add();
return 0;
}
Output :-

27. Write a C++ program function overloading finding area of


triangle,square and rectangle.
Program :-
#include<iostream>
using namespace std;

NITIN KUMAR SINGAUR 30 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

int area(int);
int area(int,int);
float area(float,float);
int main()
{
int s,l,b;
float bs,ht;
cout<<"Enter side of a square:";
cin>>s;
cout<<"Enter length and breadth of rectangle:";
cin>>l>>b;
cout<<"Enter base and height of triangle:";
cin>>bs>>ht;
cout<<"Area of square is "<<area(s);
cout<<"\nArea of rectangle is "<<area(l,b);
cout<<"\nArea of triangle is "<<area(bs,ht);
}
int area(int s)
{
return(s*s);
}
int area(int l,int b)
{
return(l*b);
}
float area(float bs,float ht)
{
return((bs*ht)/2);
}

Output :-

NITIN KUMAR SINGAUR 31 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

28. Write a C++ program to returning a structure variable from a


function.
Program :-
#include <iostream>

NITIN KUMAR SINGAUR 32 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

using namespace std;


struct Employee
{
int Id;
string Name;
};
Employee data(Employee E)
{
E.Id = 45;
E.Name = "aman";
return (E);
}
int main()
{
// creating object of Employee
Employee Emp;
// calling function data to assign value
Emp = data(Emp);
// display the output
cout << "Employee Id: " << Emp.Id;
cout << "\nEmployee Name: " << Emp.Name;
return 0;
}

Output :-

NITIN KUMAR SINGAUR 33 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

29. Write a C++ program to demonstrate “class”.


Program :-

NITIN KUMAR SINGAUR 34 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

#include <iostream>
using namespace std;
class Geeks
{
// Access specifier
public:
// Data Members
string geekname;
// Member Functions()
void printname()
{
cout << "Geekname is:" << geekname;
}
};
int main()
{
// Declare an object of class geeks
Geeks obj1;
// accessing data member
obj1.geekname = "mohan";
// accessing member function
obj1.printname();
return 0;
}
Output :-

30. Write a C++ program to make outside function inline.

NITIN KUMAR SINGAUR 35 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

Program :-
#include<iostream>
using namespace std;
class A
{
int a,b;
public:
void display(int x,int y);
};
inline void A :: display(int x,int y)
{
a=x,b=y;
cout<<a<<endl;
cout<<b;
};
int main()
{
A d;
d.display(10,20);
return 0;
}
Output :-

31. Write a C++ program to demonstrate nesting of member function.


Program :-

NITIN KUMAR SINGAUR 36 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

#include <iostream>
using namespace std;
class average
{
int a,b;
public:
void read();
void print();
int avg();
};
void average::read()
{
cout<<"\n enter a and b: ";
cin>>a>>b;
}
void average::print()
{
cout<<"value of a: "<<a;
cout<<"\nvalue of b: "<<b;
cout<<"\naverage is : "<<avg();
}
int average::avg()
{
return (a+b)/2;
}
main()
{
average A;
A.read();
A.print();
}

NITIN KUMAR SINGAUR 37 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

Output :-

32.Write a C++ program to declare public member function inside of


the class and define member function outside of the class.
Program :-

NITIN KUMAR SINGAUR 38 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

Inside Class Defination :-

#include<iostream>
using namespace std;
class car
{
private:
int car_number;
char car_model[10];
public:
void getdata()
{
cout<<"enter car number";
cin>>car_number;
cout<<"enter car model";
cin>>car_model;
}
void showdata()
{
cout<<"car number is:"<<car_number<<endl;
cout<<"car model is:"<<car_model;
}
};
int main()
{
car c1;
c1.getdata();
c1.showdata();
return 0;
}
Output :-

NITIN KUMAR SINGAUR 39 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

Outside Class Defination :-

#include<iostream>
using namespace std;
class car
{
private:
int car_number;
char car_model[10];
public:
void getdata();
void showdata();
};
void car::getdata()
{
cout<<"enter car number";
cin>>car_number;
cout<<"enter car model";
cin>>car_model;
}
void car::showdata()
{
cout<<"car number is:"<<car_number<<endl;
cout<<"car model is:"<<car_model;
}
int main()

NITIN KUMAR SINGAUR 40 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

{
car c1;
c1.getdata();
c1.showdata();
return 0;
}
Output :-

33. Write a C++ program to demonstrate array as a class member.


Program :-

NITIN KUMAR SINGAUR 41 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

#include<iostream>
using namespace std;
const int size=5;
class student
{
int roll_no;
int marks[size];
public:
void getdata ();
void tot_marks ();
};
void student :: getdata ()
{
cout<<"\nEnter roll no: ";
cin>>roll_no;
for(int i=0; i<size; i++)
{
cout<<"Enter marks in subject"<<(i+1)<<": ";
cin>>marks[i] ;
}
}
void student :: tot_marks() //calculating total marks
{
int total=0;
for(int i=0; i<size; i++)
total+= marks[i];
cout<<"\n\nTotal marks "<<total;
}
int main()
{
student stu;

NITIN KUMAR SINGAUR 42 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

stu.getdata() ;
stu.tot_marks() ;
return 0;
}
Output :-

34. Write a C++ program to demonstrate private access specifier.


Program :-

NITIN KUMAR SINGAUR 43 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

#include <iostream>
using namespace std;
class A
{
// private elements
private:
int age;
public:
void displayAge(int a)
{
age = a;
cout << "Age = " << age << endl;
}
};
int main()
{
int ageInput;
// declare an object
A obj1;
cout << "Enter your age: ";
cin >> ageInput;
// call function and pass ageInput as argument
obj1.displayAge(ageInput);
return 0;
}

Output :-

NITIN KUMAR SINGAUR 44 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

35. Write a C++ program to demonstrate public access specifier.

NITIN KUMAR SINGAUR 45 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

Program :-
#include <iostream>
using namespace std;
class A
{
public:
int age;
void displayAge()
{
cout << "Age = " << age << endl;
}
};
int main()
{
// declare a class object
A obj1;
cout << "Enter your age: ";
cin >> obj1.age;
obj1.displayAge();
return 0;
}
Output :-

36. Write a C++ program to demonstrate protected access specifier.

NITIN KUMAR SINGAUR 46 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

Program :-
#include <iostream>
using namespace std;
class A
{
protected:
int age;
};
// declare child class
class B : public A
{
public:
void displayAge(int a)
{
age = a;
cout << "Age = " << age << endl;
}
};
int main()
{
int ageInput;
// declare object of child class
B child;
cout << "Enter your age: ";
cin >> ageInput;
child.displayAge(ageInput);
return 0;
}

Output :-

NITIN KUMAR SINGAUR 47 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

37. Write a C++ program to demonstrate array as class member.

NITIN KUMAR SINGAUR 48 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

Program :-
#include<iostream>
using namespace std;
const int size=5;
class student
{
int roll_no;
int marks[size];
public:
void getdata ();
void tot_marks ();
};
void student :: getdata ()
{
cout<<"\nEnter roll no: ";
cin>>roll_no;
for(int i=0; i<size; i++)
{
cout<<"Enter marks in subject"<<(i+1)<<": ";
cin>>marks[i] ;
}
}
void student :: tot_marks() //calculating total marks
{
int total=0;
for(int i=0; i<size; i++)
total+= marks[i];
cout<<"\n\nTotal marks "<<total;
}
int main()
{
student stu;
stu.getdata() ;

NITIN KUMAR SINGAUR 49 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

stu.tot_marks() ;
return 0;
}
Output :-

38. Write a C++ program to demonstrate new and delete operator.

NITIN KUMAR SINGAUR 50 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

Program :-
#include <iostream>
using namespace std;
int main()
{
int *p1, *p2, sum;
p1=new int;
p2= new int;
cout<<" Enter first value :";
cin>>*p1;
cout<<" Enter second value :";
cin>>*p2;
sum = *p1 + *p2;
cout<<" Sum of values = "<<sum<<endl;
delete p1;
delete p2;
}
Output :-

39. Write a C++ program to demonstrate object as function argument.

NITIN KUMAR SINGAUR 51 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

Program :-
#include<iostream>
using namespace std;
class complex
{
int a;
int b;
public:
void setData(int v1, int v2)
{
a = v1;
b = v2;
}
void setDataBySum(complex o1, complex o2)
{
a = o1.a + o2.a;
b = o1.b + o2.b;
}
void printNumber()
{
cout<<"Your complex number is "<<a<<" + "<<b<<"i"<<endl;
}
};
int main()
{
complex c1, c2, c3;
c1.setData(1, 2);
c1.printNumber();

c2.setData(3, 4);
c2.printNumber();

NITIN KUMAR SINGAUR 52 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

c3.setDataBySum(c1, c2);
c3.printNumber();
return 0;
}
Output :-

40. Write a C++ program to demonstrate default constructor.

NITIN KUMAR SINGAUR 53 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

Program :-
#include<iostream>
using namespace std;
class A
{
public:
int x,y;
A()
{
x=2;
y=3;
cout<<x<<endl<<y;
}
};
int main()
{
A a;
return 0;
}

Output :-

41. Write a C++ program to demonstrate parameterized constructor.

NITIN KUMAR SINGAUR 54 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

Program :-
#include<iostream>
using namespace std;
class A
{
public:
int x,y;
A(int c,int d)
{
x=c;
y=d;
cout<<x<<endl<<y;
}
};
int main()
{
A a(10,20);
return 0;
}
Output :-

42. Write a C++ program to demonstrate copy constructor.

NITIN KUMAR SINGAUR 55 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

Program :-
#include <iostream>
using namespace std;
class A
{
public:
int x,y;
A() // default constructor.
{
x=2;
y=4;
cout<<x<<endl<<y;
}
A(A &i) // copy constructor
{
x = i.x;
y = i.y;
}
};
int main()
{
A a;
A b=a;
return 0;
}
Output :-

43. Write a C++ program to demonstrate constructor overloading.

NITIN KUMAR SINGAUR 56 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

Program :-
#include <iostream>
using namespace std;
class Person
{
private:
int age;
public:
// 1. Constructor with no arguments
Person()
{
age = 20;
}
// 2. Constructor with an argument
Person(int a)
{
age = a;
}
int getAge()
{
return age;
}
};
int main()
{
Person person1, person2(30);
cout << "Person1 Age = " << person1.getAge() << endl;
cout << "Person2 Age = " << person2.getAge() << endl;
return 0;
}
Output :-

NITIN KUMAR SINGAUR 57 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

44. Write
a C++ program to demonstrate constructor with default
argument.

NITIN KUMAR SINGAUR 58 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

Program :-
#include<iostream>
using namespace std;
class A
{
public:
int a;
A(int x, int y, int z=2)
{
a=x*y*z;
cout<<"a is:"<<a;
}
};
int main()
{
A d(10,20);
return 0;
}
Output :-

45. Write a C++ program to demonstrate single inheritance.


Program :-

NITIN KUMAR SINGAUR 59 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

#include<iostream>
using namespace std;
// base class
class Vehicle
{
public:
Vehicle()
{
cout << "This is a Vehicle\n";
}
};
// sub class derived from a single base classes
class Car : public Vehicle
{

};
// main function
int main()
{
Car obj;
return 0;
}
Output :-

46. Write a C++ program to demonstrate multiple inheritance.


Program :-

NITIN KUMAR SINGAUR 60 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

#include <iostream>
using namespace std;
// first base class
class Vehicle
{
public:
Vehicle()
{
cout << "This is a Vehicle\n";
}
};
// second base class
class FourWheeler
{
public:
FourWheeler()
{
cout << "This is a 4 wheeler Vehicle\n";
}
};
// sub class derived from two base classes
class Car : public Vehicle, public FourWheeler
{
};
int main()
{
Car obj;
return 0;

Output :-

NITIN KUMAR SINGAUR 61 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

47. Write a C++ program to demonstrate hierarchical inheritance.

NITIN KUMAR SINGAUR 62 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

Program :-
#include <iostream>
using namespace std;
// base class
class Vehicle
{
public:
Vehicle()
{
cout << "This is a Vehicle\n";
}
};
// first sub class
class Car : public Vehicle
{
};
// second sub class
class Bus : public Vehicle
{
};
// main function
int main()
{
Car obj1;
Bus obj2;
return 0;
}

Output :-

NITIN KUMAR SINGAUR 63 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

48. Write a C++ program to demonstrate function overriding.

NITIN KUMAR SINGAUR 64 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

Program :-
#include <iostream>
using namespace std;
class A
{
public:
void first_Print()
{
cout << "Base Function" << endl;
}
};
class B : public A
{
public:
void second_Print()
{
cout << "Derived Function" << endl;
}
};
int main()
{
B c;
c.second_Print();
return 0;
}
Output :-

NITIN KUMAR SINGAUR 65 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

49. Write a C++ program to create a file named “Rudra” using


constructor.
Program :-
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ofstream fout("Rudra");
char name[20];
int age;
cout<<"enter studemt name:";
cin.getline(name,20);
cout<<"enter age:";
cin>>age;
fout<<name<<endl;
fout<<age;
fout.close();

ifstream fin("Rudra");
fin.getline(name,20);
fin>>age;
cout<<name<<endl;
cout<<age;
fin.close();
return 0;

NITIN KUMAR SINGAUR 66 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

Output :-

NITIN KUMAR SINGAUR 67 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

50. Write a C++ program to create a file name “Info” using open
function having details about your name,age,class and address.and
display them into the console using eof( ).
Program :-
#include<iostream>
#include<fstream>
using namespace std;
int main()
{

char name[20] , address[20] , classname[20];


int age;
ofstream fout;
fout.open("Info");
cout<<"enter the name:"<<endl;
cin.getline(name,20);
cout<<"enter the age:"<<endl;
cin>>age;
cout<<"enter the classname:"<<endl;
cin>>classname;
cout<<"enter the address:"<<endl;
cin>>address;
fout<<name<<endl;
fout<<age<<endl;
fout<<classname<<endl;
fout<<address<<endl;
fout.close();

ifstream fin("Info");
fin.getline(name,20);
fin>>age;

NITIN KUMAR SINGAUR 68 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

fin>>classname;
fin>>address;
cout<<name<<endl;
cout<<age<<endl;
cout<<classname<<endl;
cout<<address<<endl;
fin.close();
}

Output :-

NITIN KUMAR SINGAUR 69 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ifstream fin("Info");
char c;
while(!fin.eof())
{
c=fin.get();
cout<<c;
}

}
Output :-

NITIN KUMAR SINGAUR 70 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

51. Write a C++ program to perform truncate operation in a file existing


file named “File”.
Program :-
Create a file :-
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
char data[50];
ofstream obj;
obj.open("File");
cout<<"write to the file"<<endl;
cout<<"your name is==";
cin.getline(data,50);
obj<<data<<endl;
obj.close();

ifstream obj2;
obj2.open("File");
cout<<"your data is==";
obj2>>data;
cout<<data<<endl;
obj2.close();
}

NITIN KUMAR SINGAUR 71 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

Output :-

truncate operation in a file :-


#include<iostream>
#include<fstream>
using namespace std;
int main()
{
char data[50];
ofstream obj;
obj.open("File",ios::trunc | ios::out);
cout<<"write to the file"<<endl;
cout<<"your name is==";
cin.getline(data,50);
obj<<data<<endl;
obj.close();

ifstream obj2;
obj2.open("File");
cout<<"your data is==";
obj2>>data;

NITIN KUMAR SINGAUR 72 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

cout<<data<<endl;
obj2.close();
}
Output :-

#include<iostream>
#include<fstream>
using namespace std;
int main()
{
char data[50];
ofstream obj;
obj.open("File",ios::trunc | ios::out);
cout<<"write to the file"<<endl;
cout<<"your name is==";
cin.getline(data,50);

obj<<data<<endl;
obj.close();

fstream obj2;

NITIN KUMAR SINGAUR 73 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

obj2.open("File");
cout<<"your data is==";
obj2>>data;
cout<<data<<endl;
cout<<"enter your new data"<<endl;
cin.getline(data,10);
obj2<<data;
obj2.close();
}

Output :-

NITIN KUMAR SINGAUR 74 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

52. Write a C++ program to open a existing file name “Shiva” in append
mode to add some content in a file.
Program :-
Create a File :-
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
char name[50];
int age;
ofstream obj;
obj.open("Shiva");
cout<<"enter the name:"<<endl;
cin.getline(name,50);
cout<<"enter the age:"<<endl;
cin>>age;
obj<<name<<endl;
obj<<age<<endl;
obj.close();

ifstream obj2;
obj2.open("Shiva");
obj2.getline(name,50);
obj2>>age;
cout<<name<<endl;
cout<<age<<endl;
obj2.close();
}

NITIN KUMAR SINGAUR 75 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

Output :-

appent mode :-
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
char name[50];
int age;
ofstream obj;
obj.open("Shiva",ios::app);
cout<<"enter the name:"<<endl;
cin.getline(name,50);
cout<<"enter the age:"<<endl;
cin>>age;
obj<<name<<endl;
obj<<age<<endl;
obj.close();

NITIN KUMAR SINGAUR 76 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

}
Output :-

NITIN KUMAR SINGAUR 77 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

53. Write a C++ program to find current position of input/output


pointer of a file.
Program :-
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream file;
//open file sample.txt in and Write mode
file.open("sample.txt",ios::out);
if(!file)
{
cout<<"Error in creating file!!!";
return 0;
}
file<<"This is nitin";
//print the position
cout<<"Current position is: "<<file.tellp()<<endl;
file.close();
//again open file in read mode
file.open("sample.txt",ios::in);
if(!file)
{
cout<<"Error in opening file!!!";
return 0;
}
cout<<"After opening file position is: "<<file.tellg()<<endl;
//read characters untill end of file is not found
char ch;
while(!file.eof())

NITIN KUMAR SINGAUR 78 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

{
cout<<"At position : "<<file.tellg(); //current position
file>>ch; //read character from file
cout<<" Character \""<<ch<<"\""<<endl;
}
//close the file
file.close();
return 0;
}

Output :-

NITIN KUMAR SINGAUR 79 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

NITIN KUMAR SINGAUR 80 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

54. Write a C++ program to differentiate read( ) and getline( ) function.


Program :-
#include<iostream>
using namespace std;
int main()
{
char str[15];
cout<<"\n enter the string:";
cin.getline(str,15);
cout<<"\n entered string:";
cout<<str;
cin.read(str,15);
return 0;
}

Output :-

NITIN KUMAR SINGAUR 81 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

55. Write a C++ program to demonstrate manupulators(setw,


setprecision, setbase, setfill).
Program :-
Setw() :-
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
cout<<"setw........."<<endl;
string a ="rahul";
cout<<a<<endl;
cout<<"("<<setw(9)<<a<<")"<<endl;
return 0;
}
Output :-

Setprecision() :-
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
cout<<"setprecision........."<<endl;

cout<<2.3456<<endl;

NITIN KUMAR SINGAUR 82 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

cout<<setprecision(2);
cout<<fixed<<2.3456<<endl;
return 0;
}
Output :-

Setbase() :-
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
cout<<"setbase........."<<endl;
int x=100;
cout<<setbase(8)<<x<<endl;
cout<<setbase(10)<<x<<endl;
cout<<setbase(16)<<x<<endl<<endl;
return 0;
}
Output :-

NITIN KUMAR SINGAUR 83 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

Setfill() :-
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
cout<<"setfill........."<<endl;
string a="rahul";
cout<<setfill('*');
cout<<setw(9)<<a<<endl<<endl;
return 0;
}
Output :-

NITIN KUMAR SINGAUR 84 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

56. Write a C++ program which reads input from the keyboard whose
width specified with 8 and unused space filled with '#'and input should
be left-justified.
Program :-
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
cout.width(8);
cout<<6789<<endl;
cout.fill('#');
cout<<6789<<endl;

cout<<8.799<<endl;
cout<<setprecision(3);
cout<<fixed<<8.6799<<endl;

cout.width(8);
cout.setf(ios::left);
cout<<6789<<endl;

cout.width(8);
cout.unsetf(ios::left);
cout<<6789<<endl;
}

NITIN KUMAR SINGAUR 85 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

Output :-

NITIN KUMAR SINGAUR 86 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

57. Illustrates the use of get() & put() character handling functions.
Program :-
get() :-
#include<iostream>
using namespace std;
int main()
{
char a;
cout<<"enter any charactor:";
cin.get(a);
cout<<”\n entered character:”;
cout.put(a);
return 0;
}

Output :-

NITIN KUMAR SINGAUR 87 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

58. Write a C++ program to differentiate & and *.


Program :-
#include <iostream>
using namespace std;
int main ()
{
int var;
int *ptr;
int val;
var = 3000;

// take the address of var


ptr = &var;

// take the value available at ptr


val = *ptr;
cout << "Value of var :" << var << endl;
cout << "Value of ptr :" << ptr << endl;
cout << "Value of val :" << val << endl;
return 0;
}
Output :-

NITIN KUMAR SINGAUR 88 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

59. Write a C++ program to access the address of object of a class using
“this pointer”.
Program :-
#include<iostream>
using namespace std;
class A
{
int a , b ;
public:
void data(int a , int b)
{
this->a=a+b;
this->b=a-b;
}
void display()
{
cout<<a<<endl;
cout<<b<<endl;
cout<<"object address is:"<<this;
}
};
int main()
{
A s;
s.data(12,13);
s.display();
return 0;

NITIN KUMAR SINGAUR 89 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

Output :-

NITIN KUMAR SINGAUR 90 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

60. Write a C++ program to demonstrate pointer to object.


Program :-
#include<iostream>
using namespace std;
class A
{
int a;
public:
void disp()
{
int a=8;
cout<<a;
}
};
int main()
{
A b , *ptr;
ptr=&b;
ptr->disp();
}

Output :-

NITIN KUMAR SINGAUR 91 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

61. Write a C++ program to demonstrate pointer to derived class.


Program :-
#include<iostream>
using namespace std;
class A
{
public:
void disp()
{
cout<<"this is base class"<<endl;
}
};
class B:public A
{
public:
void disp()
{
cout<<"this is child class"<<endl;
}
};
int main()
{
A a,*p;
a.disp();
p=&a;
p->disp();
cout<<"....................."<<endl;

B b, *p1;
b.disp();
p1=&b;

NITIN KUMAR SINGAUR 92 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

p->disp();
cout<<"......................."<<endl;
}
Output :-

NITIN KUMAR SINGAUR 93 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

62.Write a C++ program to demonstrate virtual function.


Program :-
#include <iostream>
using namespace std;
class Base
{
public:
virtual void print()
{
cout << "Base Function" << endl;
}
};
class Derived : public Base
{
public:
void print()
{
cout << "Derived Function" << endl;
}
};

int main()
{
Derived derived1;
// pointer of Base type that points to derived1
Base* base1 = &derived1;
// calls member function of Derived class
base1->print();
return 0;
}

NITIN KUMAR SINGAUR 94 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

Output :-

NITIN KUMAR SINGAUR 95 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

63. Write a C++ program to demonstrate pure virtual function having


base class name “shape” which contains dummy function name “area”
which has null body and two child classes inherits “shape” class named
“triangle” And “rectangle” . both child classes redefined “shape” class
dummy function to find area’a of tringle and rectangle respectively.
Program :-
#include <iostream>
class Shape
{
public:
// pure virtual function with a null body
virtual double area() = 0;
};
class Triangle : public Shape
{
private:
double base;
double height;
public:
Triangle(double base, double height) : base(base), height(height) {}
double area() override
{
return 0.5 * base * height;
}
};
class Rectangle : public Shape
{
private:
double width;
double height;
public:
Rectangle(double width, double height) : width(width), height(height) {}

NITIN KUMAR SINGAUR 96 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

double area() override


{
return width * height;
}
};
int main()
{
Triangle triangle(10, 20);
std::cout<< "Area of triangle: " <<triangle.area() << std::endl;
Rectangle rectangle(10, 20);
std::cout<< "Area of rectangle: " <<rectangle.area() << std::endl;
return 0;
}

Output :-

NITIN KUMAR SINGAUR 97 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

64. Write a C++ program to overload unary + operator using member


function.
Program :-
#include <iostream>
using namespace std;
class A
{
private:
int a,b;
public:
A(int x , int y)
{
a = x;
b = y;
cout<<"before overloading a and b is:"<<a<<b<<endl;
}
void operator -()
{
a = -a;
b = -b;
cout<<"\nvalue of a and b:"<<a<<b;
}

};
int main()
{
A n(10,20);
-n;
return 0;
}

NITIN KUMAR SINGAUR 98 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

Output :-

NITIN KUMAR SINGAUR 99 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

65. Write a C++ program to overload unary + operator using friend


function.
Program :-
#include <iostream>
using namespace std;
class A
{
private:
int a,b;
public:
A(int x , int y)
{
a = x;
b = y;
cout<<"before overloading a and b is:"<<a<<b<<endl;
}
friend void operator -(A o)
{
o.a = -o.a;
o.b = -o.b;
cout<<"\nvalue of a and b:"<<o.a<<o.b;
}
};
int main()
{
A o(10,20);
-o;
return 0;
}

NITIN KUMAR SINGAUR 100 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

Output :-

NITIN KUMAR SINGAUR 101 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

66. Write a C++ program to overload unary ++ operator using member


function.
Program :-
#include <iostream>
using namespace std;
class A
{

public:
void operator ++()
{
int a = 8;
cout<<"a is:"<<a<<endl;
a=++a;
cout<<"pre increament value of a:"<<a<<endl;

int b;
b=a++;
cout<<"value of b is:"<<b<<endl;
}
};
int main()
{
A A;
++A;

NITIN KUMAR SINGAUR 102 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

Output :-

NITIN KUMAR SINGAUR 103 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

67. Write a C++ program to overload binary + operator using member


function for addition of two complex number with member function.
Program :-
#include<iostream>
using namespace std;
class Complex
{
int num1, num2;
public:
void accept()
{
cout<<"\n Enter Two Complex Numbers : ";
cin>>num1>>num2;
}
Complex operator+(Complex obj)
{
Complex c;
c.num1=num1+obj.num1;
c.num2=num2+obj.num2;
return(c);
}
void display()
{
cout<<num1<<"+"<<num2<<"i"<<"\n";
}
};
int main()
{
Complex c1, c2, sum;

c1.accept();

NITIN KUMAR SINGAUR 104 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

c2.accept();
sum = c1+c2;
cout<<"\n Entered Values : \n";
cout<<"\t";
c1.display();
cout<<"\t";
c2.display();
cout<<"\n Addition of Real and Imaginary Numbers : \n";
cout<<"\t";
sum.display();
return 0;
}
Output :-

NITIN KUMAR SINGAUR 105 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

68. Write a C++ program to overload binary + operator using member


function for addition of two complex number with friend function.
Program :-
#include<iostream>
using namespace std;
class Complex
{
int num1, num2;
public:
void accept()
{
cout<<"\n Enter Two Complex Numbers : ";
cin>>num1>>num2;
}
//Overloading '+' operator using Friend function
friend Complex operator+(Complex c1, Complex c2);

void display()
{
cout<<num1<<"+"<<num2<<"i"<<"\n";
}
};
Complex operator+(Complex c1, Complex c2)
{
Complex c;
c.num1=c1.num1+c2.num1;
c.num2=c1.num2+c2.num2;
return(c);
}
int main()
{

NITIN KUMAR SINGAUR 106 | P a g e


PROGRAMMING IN C++ MCA 1st SEMESTER

Complex c1,c2, sum;


c1.accept();
c2.accept();
sum = c1+c2;
cout<<"\n Entered Values : \n";
cout<<"\t";
c1.display();
cout<<"\t";
c2.display();
cout<<"\n Addition of Real and Imaginary Numbers : \n";
cout<<"\t";
sum.display();
return 0;
}

Output :-

NITIN KUMAR SINGAUR 107 | P a g e

You might also like