Professional Documents
Culture Documents
mca 1st c++-1
mca 1st c++-1
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 :-
Endl-
#include<iostream>
using namespace std;
int main()
{
int age;
cout<<"c++ tutorial:";
cout<<"books"<<endl;
cout<<"end of line"<<endl;
}
Output :-
Global variable –
#include<iostream>
using namespace std;
int a=10;
int main()
{
int a,b,c;
b=5;
c=6;
a=b+c;
cout<<"a is"<<a<<endl;
cout<<"global variable is:"<<::a;
return 0;
}
Output :-
#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 :-
Output :-
return 0;
}
Output :-
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;
Output :-
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 :-
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 :-
#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 :-
Output :-
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 :-
// 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;
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>
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 :-
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 :-
Output :-
#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 :-
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 :-
#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();
}
Output :-
#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 :-
#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()
{
car c1;
c1.getdata();
c1.showdata();
return 0;
}
Output :-
#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() ;
stu.tot_marks() ;
return 0;
}
Output :-
#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 :-
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 :-
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 :-
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() ;
stu.tot_marks() ;
return 0;
}
Output :-
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 :-
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();
c3.setDataBySum(c1, c2);
c3.printNumber();
return 0;
}
Output :-
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 :-
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 :-
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 :-
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 :-
44. Write
a C++ program to demonstrate constructor with default
argument.
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 :-
#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 :-
#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 :-
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 :-
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 :-
ifstream fin("Rudra");
fin.getline(name,20);
fin>>age;
cout<<name<<endl;
cout<<age;
fin.close();
return 0;
Output :-
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()
{
ifstream fin("Info");
fin.getline(name,20);
fin>>age;
fin>>classname;
fin>>address;
cout<<name<<endl;
cout<<age<<endl;
cout<<classname<<endl;
cout<<address<<endl;
fin.close();
}
Output :-
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ifstream fin("Info");
char c;
while(!fin.eof())
{
c=fin.get();
cout<<c;
}
}
Output :-
ifstream obj2;
obj2.open("File");
cout<<"your data is==";
obj2>>data;
cout<<data<<endl;
obj2.close();
}
Output :-
ifstream obj2;
obj2.open("File");
cout<<"your data is==";
obj2>>data;
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;
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 :-
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();
}
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();
}
Output :-
{
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 :-
Output :-
Setprecision() :-
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
cout<<"setprecision........."<<endl;
cout<<2.3456<<endl;
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 :-
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 :-
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;
}
Output :-
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 :-
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;
Output :-
Output :-
B b, *p1;
b.disp();
p1=&b;
p->disp();
cout<<"......................."<<endl;
}
Output :-
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;
}
Output :-
Output :-
};
int main()
{
A n(10,20);
-n;
return 0;
}
Output :-
Output :-
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;
Output :-
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 :-
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()
{
Output :-