#include<iostream>
using namespace std;
class Math
{
int num1,num2,num3;
public:
void input()
{
cout<<"Enter first number: ";
cin>>num1;
cout<<"Enter second number: ";
cin>>num2;
}
void sum()
{
num3=num1+num2;
cout<<"Sum is "<<num3;
}
};//end of class
int main()
{
Math mt;
mt.input();
mt.sum();
return 0;
}
#include<iostream>
using namespace std;
class Student
{
char name[10];
int rollno;
float fee;
public:
void input();
void output();
};//end of class
void Student::input()
{
cout<<"Enter name ";
cin>>name;
cout<<"Enter rollno ";
cin>>rollno;
cout<<"Enter fees ";
cin>>fee;
}
void Student::output()
{
cout<<"\nName is "<<name;
cout<<"\nRollno is "<<rollno;
cout<<"\nFee is "<<fee;
}
int main()
{
Student st;
st.input();
st.output();
return 0;
}
#include<iostream> using namespace std; class Student
{
int rollno,sum;
float marks[2],per;
public:
void get_data()
{
int i;
sum=0;
cout<<"Enter rollno ";
cin>>rollno;
for(i=0;i<2;i++)
{
cout<<"Enter marks ";
cin>>marks[i];
sum=sum+marks[i];
}
}
void percent()
{
per=sum/2;
cout<<"\nPercentage is "<<per;
}};
int main()
{
Student st[2];
int i;
for(i=0;i<2;i++) { st[i].get_data(); cout<<"\nPercentage of student "<<(i+1); st[i].percent(); cout<<endl<<"----------------------------------"<<endl;
}
return 0; }
#include<iostream> using namespace std; class Demo
{
int num;
public:
void get_data()
{
cout<<"Enter number ";
cin>>num;
}
void sum(Demo dm)
{
int res;
res=num+dm.num;
cout<<"Sum is "<<res;
} };
int main()
{
Demo d1,d2;
d1.get_data();
d2.get_data();
d1.sum(d2);
return 0;
}
#include<iostream> using namespace std; class Demo
{
int num;
public:
void get_data()
{
cout<<"Enter number ";
cin>>num;
}
void sum(Demo &dm)
{
int temp;
cout<<"\nValues before swapping are "<<num<<" and "<<dm.num;
temp=num;
num=dm.num;
dm.num=temp;
cout<<"\nValues after swapping are "<<num<<" and "<<dm.num;
}
};
int main()
{
Demo d1,d2;
d1.get_data();
d2.get_data();
d1.sum(d2);
return 0; }
#include<iostream> using namespace std; class Demo
{
int num1,num2,num3;
public:
void max();
};
inline void Demo::max()
{
cout<<"Enter first number ";
cin>>num1;
cout<<"Enter second number ";
cin>>num2;
cout<<"Enter third number ";
cin>>num3;
if(num1>num2 && num1>num3)
{
cout<<"num1 is greater ";
}
else if(num2>num1 && num2>num3)
{
cout<<"num2 is greater ";
}
else
{
cout<<"num3 is greater ";
}
}
int main()
{
Demo d1;
d1.max();
return 0;
}
#include<iostream> using namespace std; class Rectangle
{
private:
int length;
int breadth;
public:
Rectangle()
{
length=10;
breadth=20;
cout<<"The length of the rectangle is: "<<length<<endl;
cout<<"The breadth of the rectangle is: "<<breadth;
}
};//end of class
int main()
{
Rectangle rect;
return 0;
}
#include<iostream> using namespace std; class Rectangle
{
private:
int length;
int breadth;
int rect;
public:
Rectangle(int l,int b)
{
length=l;
breadth=b;
}
void area()
{
rect=length*breadth;
cout<<"The area of the rectangle is:"<<rect;
}
};//end of class
int main()
{
Rectangle rect1(10,10);
Rectangle rect2(40,60);
rect1.area();
rect2.area();
return 0; }
#include<iostream> using namespace std; class Rectangle
{
private:
int length;
int breadth;
int rect;
public:
Rectangle()
{
length=100;
breadth=200;
cout<<"This is the length and breadth of the default constructor "<<endl;
cout<<"The length of the rectangle is: "<<length<<endl;
cout<<"The breadth of the rectangle is: "<<breadth<<endl;
}
Rectangle(int l,int b)
{
length=l;
breadth=b;
rect=length*breadth;
cout<<"This is parameterized constructor"<<endl;
cout<<"The area of the rectangle is: "<<rect;
}
};//end of class
int main()
{
Rectangle r;
Rectangle r1(10,10); return 0; }
#include<iostream> using namespace std; class Rectangle
{
private:
int length;
int breadth;
int area;
public:
void input()
{
cout<<"Enter the length of a rectangle ";
cin>>length;
cout<<"Enter the breadth of a rectangle ";
cin>>breadth;
}
void copyValue(Rectangle r)
{
length=r.length;
breadth=r.breadth;
}
void calcArea()
{
area=length*breadth;
cout<<"\n Area of rectangle is "<<area;
}
};//end of class
int main()
{
Rectangle r1,r2;
r1.input();
r2.copyValue(r1);
r1.calcArea();
r2.calcArea(); return 0; }
#include<iostream> using namespace std; class Demo
{
int num;
public:
Demo()
{
num=100;
cout<<"Default constructor "<<num;
}
~Demo()
{
cout<<"\nThis is Destructor ";
}
};
int main()
{
Demo dm; return 0; }
#include<iostream> using namespace std; class Math
{
private:
int num1;
int num2;
int num3;
float n1,n2,n3;
public:
/*void sum(int a1,int a2)
{
num1=a1;
num2=a2;
num3=num1+num2;
cout<<"The sum of two number is:"<<num3;
}*/
void sum(float f1,float f2)
{
n1=f1;
n2=f2;
n3=n1+n2;
cout<<"\nThe sum of two number is:"<<n3;
}
};
int main()
{
Math m1;
m1.sum(10,20);
m1.sum(10.1,20.1); return 0; }
#include<iostream> using namespace std; class Area
{
private:
int length;
int breadth;
int ar;
int side;
public:
void area(int l,int b)
{
length=l;
breadth=b;
ar=length*breadth;
cout<<"The area of the rectangle is:";
cout<<ar;
cout<<"\n";
}
void area(int s)
{
side=s;
ar=side*side;
cout<<"The area of the square is:";
cout<<ar;
}
};
int main()
{
Area a1;
a1.area(10,10);
a1.area(12); return 0; }
#include<iostream> using namespace std; class Maths
{
int num;
public:
friend Maths operator+(Maths,Maths);
void input()
{
cout<<"Enter number ";
cin>>num;
}
void display()
{
cout<<"Value after addition "<<num;
}
};
Maths operator+(Maths m1,Maths m2)
{
Maths temp;
temp.num=m1.num+m2.num;
return temp;
}
int main()
{
Maths m1,m2,m3;
int ans;
m1.input();
m2.input();
m3=m1+m2;
m3.display(); return 0; }
#include<iostream> using namespace std; class Maths
{
int num;
public:
friend int operator>(Maths,Maths);
void input()
{
cout<<"Enter number ";
cin>>num;
}
};
int operator>(Maths m1,Maths m2)
{ if(m1.num>m2.num)
{
return 1;
}
else
{
return 0;
} }
int main()
{
Maths m1,m2;
m1.input();
m2.input();
if(m1>m2)
{
cout<<endl<<"m1 is greater ";
}
else
{
cout<<endl<<"m2 is greater ";
}
return 0; }
#include<iostream> using namespace std; class Maths
{
int num;
public:
friend int operator==(Maths,Maths);
void input()
{
cout<<"Enter number ";
cin>>num;
}
};
int operator==(Maths m1,Maths m2)
{ if(m1.num==m2.num)
{
return 1;
}
else
{
return 0;
} }
int main()
{
Maths m1,m2;
m1.input();
m2.input();
if(m1==m2)
{
cout<<endl<<"both objects are equal ";
}
else
{
cout<<endl<<"both objects are not equal ";
} return 0; }
#include<iostream> using namespace std; class Demo
{
int num1;
int num2;
friend void usedata();
public:
void input()
{
cout<<"Enter first number: ";
cin>>num1;
cout<<"Enter second number: ";
cin>>num2;
}
void display()
{
cout<<"num1= "<<num1<<" num2= "<<num2;
}
}; void usedata()
{
Demo d1;
d1.num1=100;
d1.num2=200;
cout<<"\n The values of num1 & num2 in usedata function are: ";
cout<<endl<<"num1= "<<d1.num1<<" num2= "<<d1.num2;
} int main()
{
Demo d;
d.input();
d.display();
usedata();/* This function is only the friend function not
the member function of class Demo.So, it will
not be called with an object of a class Demo.*/ return 0; }
#include<iostream> using namespace std; class Demo
{
int num1;
int num2;
public:
friend void sum();
void input()
{
cout<<"Enter first number: ";
cin>>num1;
cout<<"Enter second number: ";
cin>>num2;
}
void output()
{
cout<<"num1= "<<num1<<" num2= "<<num2;
}
}; void sum()
{
Demo d1;
int res;
d1.input();
res=d1.num1+d1.num2;
cout<<"The sum of num1 & num2 in sum function is: "<<res;
} int main()
{
Demo d;
d.input();
d.output();
cout<<endl<<endl;
sum();
return 0;
}
#include<iostream> using namespace std; class Demo1
{
int num1;
public:
friend void sum();
void input1()
{
cout<<"Enter the value for num1 in Demo1 class ";
cin>>num1;
}
};//end of Demo1
class Demo2
{
int num2;
public:
friend void sum();
void input2()
{
cout<<"Enter the value for num2 in Demo2 class ";
cin>>num2;
}
};//end of Demo2
void sum()
{
Demo1 d1;
Demo2 d2;
int res;
d1.input1();
d2.input2();
res=d1.num1+d2.num2;
cout<<"\n Sum of Demo1-:num1 and Demo2-:num2 is: "<<res;
} int main()
{
sum();//calling of friend function sum() return 0; }
#include<iostream> using namespace std; class Test1
{
int num1;
public:
friend void greater_fun();
void input1()
{
cout<<"Enter the value for num1 in Test1 class ";
cin>>num1;
}
};//end of Test1
class Test2
{
int num2;
public:
friend void greater_fun();
void input2()
{
cout<<"Enter the value for num2 in Test2 class ";
cin>>num2;
}
};//end of Test2
void greater_fun()
{
Test1 t1;
Test2 t2;
t1.input1();
t2.input2();
if(t1.num1>t2.num2)
{
cout<<"\n Test1 class num1 is greater "<<t1.num1;
}
else
{
cout<<"\n Test2 class num2 is greater "<<t2.num2;
}
}int main()
{
greater_fun(); return 0; }
#include<iostream> using namespace std; class Maths1
{
int num1;
public:
friend class Maths3;
void input1()
{
cout<<"Enter the value for num1 in Maths1 class ";
cin>>num1;
cout<<endl;
}
};//end of Maths1
class Maths2
{
int num2;
public:
friend class Maths3;
void input2()
{
cout<<"Enter the value for num2 in Maths2 class ";
cin>>num2;
cout<<endl;
}
};//end of Maths2
class Maths3
{
Maths1 m1;
Maths2 m2;
public:
void swap1()
{
m1.input1();
m2.input2();
int temp;
cout<<"\n Values of Maths1 class num1 & Maths2 class num2 before swapping are : \n";
cout<<"Maths1 num1= "<<m1.num1;
cout<<"\nMaths2 num2= "<<m2.num2;
cout<<endl<<endl;
temp=m1.num1;
m1.num1=m2.num2;
m2.num2=temp;
cout<<"\n Values of Maths1 class num1 & Maths2 class num2 after swapping are : \n";
cout<<"Maths1 num1= "<<m1.num1;
cout<<"\nMaths2 num2= "<<m2.num2;
}
};//end of Maths3
int main()
{
Maths3 m3;
m3.swap1(); return 0; }
#include<iostream> using namespace std; class Sample
{
static int n;
public:
Sample()
{
n++;
cout<<"Object "<<n<<" created"<<endl;
}
};//end of class
int Sample::n=0;int main()
{
Sample s1,s2,s3; return 0; }
#include<iostream> using namespace std; class Rectangle
{
static int n;
int length;
int breadth;
int ar;
public:
Rectangle()
{
n++;
cout<<endl<<n<<" rectangle is created";
}
void input()
{
cout<<"\n\nEnter the length and breadth of Rectangle ";
cin>>length>>breadth;
}
void area()
{
ar=length*breadth;
cout<<"\n Area of rectangle is "<<ar;
}
};int Rectangle::n;int main()
{
Rectangle r1,r2,r3;
r1.input();
r1.area(); return 0; }
#include<iostream> using namespace std; class Rectangle
{
static int n;
int length;
int breadth;
int ar;
public:
void init()
{
// n=0;
n++;
cout<<"\n"<<n<<" rectangle is created";
cout<<"\n\nEnter the length and breadth of Rectangle ";
cin>>length>>breadth;
}
void area()
{
ar=length*breadth;
cout<<"\n Area of rectangle is "<<ar;
}
}; int Rectangle::n;int main()
{
Rectangle r1,r2,r3;
r1.init();
r1.area();
r2.init();
r2.area();
r3.init();
r3.area(); return 0; }
#include<iostream> using namespace std; class Student
{
private:
int rollno;
char name[20];
public:
void get_data();
void show_data();
};void Student::get_data()
{
cout<<"Enter the rollno. of the student:";
cin>>rollno;
cout<<"Enter the name of the student:";
cin>>name;
}
void Student::show_data()
{
cout<<"\nThe rollno of the student: "<<rollno;
cout<<"\nThe name of the student: "<<name;
}
class Marks:public Student
{
private:
float marks[5];
public:
void get_marks();
void percentage();
};
void Marks::get_marks()
{
int i;
cout<<"\nEnter marks : ";
for(i=0;i<5;i++)
{
cin>>marks[i];
}
}
void Marks::percentage()
{
int sum=0;
int i;
int pctage;
for(i=0;i<5;i++)
{
cout<<endl<<"Marks are: "<<marks[i];
sum=sum+marks[i];
}
pctage=sum/5;
cout<<endl<<"Percentage is "<<pctage<<"%";
}
int main()
{
Marks m1;
m1.get_data();
m1.show_data();
m1.get_marks();
m1.percentage(); return 0; }
#include<iostream> using namespace std; class Student
{
private:
int rollno;
char name[20];
public:
void get_data()
{
cout<<"Enter the roll no of the student";
cin>>rollno;
cout<<"Enter the name of the student";
cin>>name;
}
void show_data()
{
cout<<"\nThe roll no of the student is:";
cout<<rollno;
cout<<"\nThe name of the student is";
cout<<name;
cout<<"\n\n";
}
};//end of class student
class Teacher
{
private:
char name[20];
float salary;
char sub[10];
public:
void get_data1()
{
cout<<"Enter the name of the teacher";
cin>>name;
cout<<"Enter the salary of the teacher";
cin>>salary;
cout<<"Enter the subject of the teacher";
cin>>sub;
}
void show_data1()
{
cout<<"\nThe name of the teacher is:";
cout<<name;
cout<<"\nThe salary of the teacher is:";
cout<<salary;
cout<<"\nThe subject of the teacher is:";
cout<<sub;
cout<<"\n";
}
};
class School:public Student,public Teacher
{
private:
int no_of_student;
int no_of_teacher;
public:
void get_data2()
{
cout<<"Enter the no. of student";
cin>>no_of_student;
cout<<"\n";
cout<<"Enter the no. of the teacher";
cin>>no_of_teacher;
}
void show_data2()
{
cout<<"\nThe no. of the students are:";
cout<<no_of_student;
cout<<"\nThe no. of the teachers are:";
cout<<no_of_teacher;
}
};
int main()
{
School s1;
s1.get_data();
s1.show_data();
s1.get_data1();
s1.show_data1();
s1.get_data2();
s1.show_data2(); return 0; }
#include<iostream> using namespace std; class Student
{
private:
int rollno;
char name[20];
public:
void get_data()
{
cout<<"Enter the roll no of the student: ";
cin>>rollno;
cout<<"Enter the name of the student: ";
cin>>name;
}
void show_data()
{
cout<<"The roll no of the student is: "<<rollno<<endl;
cout<<"The name of the student is: "<<name<<endl<<endl;
}
};
class Attendance:public Student
{
protected:
int no_of_absentees;
public:
void get_data()
{
cout<<"Enter the no. of absentees: ";
cin>>no_of_absentees;
}
void show_data()
{
cout<<"The no.of absentees are: ";
cout<<no_of_absentees;
}
};
class Accounts:public Attendance
{
private:
float fees;
float bal;
public:
void get_data()
{
cout<<"\n";
cout<<"Enter the fees of the student is: ";
cin>>fees;
cout<<"Enter the bal of the student is: ";
cin>>bal;
}
void cal_total_amt()
{
int total_fees;
total_fees=fees-bal;
cout<<"The total fees of the student is: ";
cout<<total_fees;
}
};
int main()
{
Accounts a1;
a1.Student::get_data();
a1.Student::show_data();
a1.Attendance::get_data();
a1.Attendance::show_data();
a1.Accounts::get_data();
a1.cal_total_amt(); return 0; }
#include<iostream> using namespace std; class Student
{
protected:
int rollno;
char name[20];
public:
void get_data()
{
cout<<"Enter the roll no of the student ";
cin>>rollno;
cout<<"Enter the name of the student ";
cin>>name;
}
void show_data()
{
cout<<"The roll no of the student is: "<<rollno<<endl;
cout<<"The name of the student is "<<name<<endl<<endl;
}
};
class Marks:public Student
{
private:
float marks[5];
public:
void get_marks();
void percentage();
};
void Marks::get_marks()
{
int i;
cout<<"Enter the marks of the student: ";
for(i=0;i<5;i++)
{
cin>>marks[i];
}}
void Marks::percentage()
{
int sum=0;
int i;
int pctage;
for(i=0;i<5;i++)
{
cout<<marks[i]<<" ";
sum=sum+marks[i];
}
pctage=sum/5;
cout<<"\n";
cout<<"The percentage of the student is: "<<pctage<<endl<<endl;
};
class Sports:public Student
{
private:
char game_name[10];
float score;
public:
void get_score()
{
cout<<"Enter the name of the game: ";
cin>>game_name;
cout<<"\n";
cout<<"Enter the score of the game: ";
cin>>score;
cout<<"\n";
}
void show_score()
{
cout<<"The name of the game is: "<<game_name<<endl;
cout<<"The score of the game is: "<<score;
}
};
int main()
{
Marks m1;
Sports s1;
m1.get_data();
m1.show_data();
m1.get_marks();
m1.percentage();
s1.get_data();
s1.show_data();
s1.get_score();
s1.show_score(); return 0; }
#include<iostream> using namespace std; class A
{
public:
void show()
{
cout<<"\nHere is the base class";
}
};
class B:virtual public A
{
public:
void show1()
{
cout<<"\nHere is the show1 function of B class";
}};
class C:virtual public A
{
public:
void show2()
{
cout<<"\nHere is the show2 function of c class";
}
};
class D:public B,public C
{
public:
void show3()
{
cout<<"\nHere is the show3 fuction of d class";
}
};
int main()
{
D d1;
d1.show();
d1.show1();
d1.show2();
d1.show3(); return 0; }
#include<iostream> using namespace std; class Student { protected: int rollno; char name[20]; public: void get_data() { cout<<"Enter the roll no of the student "; cin>>rollno; cout<<"Enter the name of the student "; cin>>name; } void show_data() { cout<<"The roll no of the student is: "; cout<<rollno; cout<<"\n"; cout<<"The name of the student is "; cout<<name; cout<<"\n\n"; } }; class Test:public Student { protected: float marks[5]; public: void gettest(); void showtest(); }; void Test::gettest() { int i; for(i=0;i<5;i++) { cout<<"Enter the marks of the students: "; cin>>marks[i]; //cout<<"\n"; }} void Test::showtest() { int i; cout<<"The marks of the student: "; for(i=0;i<5;i++) { cout<<endl<<marks[i]; } } class Address { private: char city[10]; public: void get_add() { cout<<"\n\nEnter the name of the city "; cin>>city; } void show_add() { cout<<"\nThe name of the city is: "; cout<<city; } }; class Result:public Test,public Address { private: float total; float avg; public: Result() { total=0; } void get_result(); void show_result() { cout<<"\nThe total marks of the student: "; cout<<total; cout<<"\n"; cout<<"The avg of the student is: "; cout<<avg; }}; void Result::get_result() { int i; for(i=0;i<5;i++) { total=total+marks[i]; } avg= total/5; } int main() { Result r1; r1.get_data(); r1.show_data(); r1.gettest(); r1.showtest(); r1.get_result(); r1.show_result(); r1.get_add(); r1.show_add(); return 0; }
//program to accept a character from the user #include<iostream> using namespace std; class base
{
public:
virtual void show()
{
cout<<" \n show function of base class ";
}
};class derived:public base
{
public:
void show()
{
cout<<" \n show function of derived class ";
}
};int main()
{
derived d;
base *bptr;
bptr=&d;
bptr->show(); return 0; }
//Program to accept a character in lower case and display it in uppercase. #include<iostream> using namespace std; class Shape
{
virtual void area()=0;
virtual void perimeter()=0;
}; class Rectangle:public Shape
{
int length,breadth,ar,per;
public:
Rectangle()
{
cout<<"Enter length and breadth of rectangle: ";
cin>>length>>breadth;
}
void area()
{
ar=length*breadth;
cout<<"\n Area of rectangle is "<<ar;
}
void perimeter()
{
per=2*(length+breadth);
cout<<"\n Perimeter of rectangle is "<<per;
}
}; class Square:public Shape
{
int side,ar,per;
public:
Square()
{
cout<<"Enter side of square: ";
cin>>side;
}
void area()
{
ar=side*side;
cout<<"\n\n Area of square is "<<ar;
}
void perimeter()
{
per=4*side;
cout<<"\n Perimeter of square is "<<per;
}
};
int main()
{
Rectangle rect;
Square sq;
rect.area();
rect.perimeter();
sq.area();
sq.perimeter(); return 0; }
#include<iostream> #include<fstream> using namespace std; int main() { int num;
ofstream fout;
fout.open("c:/website/cplusassignments/abc.txt");
cout<<"Enter any number ";
cin>>num;
fout<<num;
cout<<"Data saved ";
fout.close(); return 0; }//end of main
#include<iostream> #include<fstream> using namespace std; int main() { int num1,num2;
ofstream fout;
fout.open("e:/website/cplusassignments/abc.txt");
cout<<"Enter first number ";
cin>>num1;
cout<<"Enter second number ";
cin>>num2;
fout<<num1<<"\t"<<num2;
cout<<"Data saved ";
fout.close(); return 0; }//end of main
#include<iostream> #include<fstream> using namespace std; int main() { int rollno;
char name[20];
ofstream fout("c:/website/cplusassignments/first.txt");
//calling of parameterized constructor
//fout.open("c:/website/cplusassignments/first.txt");
cout<<"Enter name ";
cin.getline(name,20);
cout<<"Enter rollno ";
cin>>rollno;
fout<<rollno<<"\t"<<name;
cout<<"Data saved ";
fout.close(); return 0; }//end of main
#include<iostream> #include<fstream> using namespace std; #include<stdlib int main() { int num;
ifstream fin("c:/website/cplusassignments/abc.txt");
fin>>num;
cout<<"Number is "<<num;
fin.close(); return 0; }//end of main
#include<iostream> #include<fstream>
using namespace std; int main()
{
int rollno;
char name[30];
fstream finout("c:/website/cplusassignments/first.txt",ios::in);
finout>>rollno;
finout.getline(name,20);
cout<<"\n Rollno is "<<rollno;
cout<<"\n Name is "<<name;
finout.close(); return 0; }//end of main
#include<iostream> #include<fstream>
using namespace std;int main()
{
char str[100];
int i=1,n=3;
fstream finout;
while(i<=n)
{
finout.open("c:/website/cplusassignments/second.txt",ios::app);
cout<<"Enter a string ";
cin.getline(str,100);
finout<<str<<endl;
i++;
}
finout.close(); return 0; }//end of main
#include<iostream> #include<fstream>
using namespace std;
int main()
{
int num;
char str[20];
fstream finout("c:/website/cplusassignments/first.txt",ios::out);
cout<<"Enter number and a string ";
cin>>num>>str;
finout<<num<<" "<<str;
cout<<"\n Data saved ";
finout.close();
finout.open("c:/website/cplusassignments/first.txt",ios::in);
finout>>num>>str;
finout.close();
finout.open("c:/website/cplusassignments/second.txt",ios::out);
finout<<num<<" "<<str;
cout<<"Number is "<<num;
cout<<"\n string is "<<str;
finout.close(); return 0; }//end of main
#include<iostream> #include<fstream>
using namespace std;
int main()
{
int num,n;
fstream finout("c:/website/cplusassignments/forth.txt",ios::out);
cout<<"Enter a number ";
cin>>num;
finout<<num;
cout<<"Data saved ";
finout.close();
finout.open("c:/website/cplusassignments/forth.txt",ios::in);
finout>>n;
cout<<"\n Number is "<<n;
finout.close(); return 0; }//end of main
#include<iostream> #include<fstream>
using namespace std;
class File
{
int rollno;
char name[10];
public:
void input()
{
cout<<"Enter name ";
cin.getline(name,20);
cout<<"Enter rollno ";
cin>>rollno;
}
void write()
{
ofstream fout("c:/website/cplusassignments/third.txt");
fout<<rollno<<"\t"<<name;
cout<<"Data saved ";
fout.close();
}
};
int main()
{
File f;
f.input();
f.write(); return 0; }//end of main
#include<iostream> #include<fstream>
using namespace std;
class File
{
int rollno;
char name[10];
public:
void read()
{
ifstream fin("c:/website/cplusassignments/third.txt");
fin>>rollno>>name;
cout<<"Rollno is : "<<rollno;
cout<<"\nName is : "<<name;
fin.close();
}
};
int main()
{
File f;
f.read(); return 0; }//end of main
#include<iostream> using namespace std; class Test
{
public:
void show()
{
cout<<"Show function of Test class";
}
};//end of classint main()
{
Test *ptr;
ptr=new Test();
//dynamic memory allocation using new operator
ptr->show(); return 0; }
#include<iostream> using namespace std; class Test
{
public:
void show()
{
cout<<"Show function of Test class";
}
};//end of classint main()
{
Test *ptr;
ptr=new Test();
//dynamic memory allocation using new operator
ptr->show(); delete ptr; //delete operator will delete the memory return 0; }
#include<iostream> using namespace std; int main()
{
int *ptr1,*ptr2;
int num1,num2,res;
cout<<"Enter first number ";
cin>>num1;
cout<<"Enter second number ";
cin>>num2;
ptr1=new int(num1);
ptr2=new int(num2);
res=*ptr1+*ptr2;
cout<<"Result is "<<res; return 0; }
#include<iostream> using namespace std; int main()
{
int i,n;
cout<<"How many numbers you want to enter ?"<<endl;
cin>>n;
int *arr=new int(n);
cout<<"Enter "<<n<<" numbers"<<endl;
for(i=0;i<n;i++)
{
cin>>arr[i];
}
cout<<"Reverse of array elements are "<<endl;
for(i=n-1;i>=0;i--)
{
cout<<arr[i]<<" ";
}
cout<<endl;
delete[] arr; return 0; }
#include<iostream> using namespace std; class Demo
{
private:
int num;
public:
void input()
{
cout<<"Enter the value for num ";
cin>>num;
}
void display()
{
cout<<"\nValue of num = "<<num;
}
};
int main()
{
Demo *ptr=new Demo;
ptr->input();
ptr->display(); delete ptr; return 0; }
#include<iostream> using namespace std; int main()
{
int *ptr1=new int(10);
int *ptr2=new int(20);
int temp;
cout<<"Values before swapping are: "<<*ptr1<<" and "<<*ptr2;
temp=*ptr1;
*ptr1=*ptr2;
*ptr2=temp;
cout<<"\nValues after swapping are: "<<*ptr1<<" and "<<*ptr2;
delete ptr1,ptr2; return 0; }
#include<iostream> using namespace std; int main()
{
int *m=new int(2);
int *n=new int(3);
int *res=new int(1);
int i;
for(i=1;i<=*n;i++)
{
*res=(*m) * (*res);
}
cout<<*res;
delete m,n,res; return 0; }
#include<iostream> using namespace std; int main()
{
int i,n,large=0;
cout<<"How many numbers you want to enter ?"<<endl;
cin>>n;
int *arr=new int(n);
cout<<"Enter "<<n<<" numbers"<<endl;
for(i=0;i<n;i++)
{
cin>>arr[i];
} for(i=0;i<n;i++)
{
if(arr[i]>large)
{
large=arr[i];
}
}
cout<<"\nLargest element of an array is "<<large; return 0; }