Practical File
Practical File
Practical File
{
Student st[10];
int n, i;
cout << "Enter total number of students:->";
cin >> n;
for (i = 0; i < n; i++)
{
cout << "Enter details of student:->" << i + 1 << "\n";
st[i].ReadDetails();
}
for (i = 0; i < n; i++)
{
cout << "Details of Student " << i + 1 << "\n";
st[i].PrintDetail();
st[i].GradeCalculate();
}
return 0;
}
4:->Write a program to declare Struct.Intialize and display contents
of member variables.
#include <iostream>
struct college_info
{
char college_name[15];
char college_code[2];
char dept[50];
int intake;
};
int main()
{
struct college_info college;
return 0;
}
5:->Write a program to declare a class and pointer to a
class.Intialise and display contents of class member.
#include <iostream>
#include <iomanip>
using namespace std;
class Complex
{
int real, imaginary;
public:
void getData()
{
cout << "The real part is " << real << endl;
cout << "The imaginary part is " << imaginary << endl;
}
void setData(int a, int b)
{
real = a;
imaginary = b;
}
};
int main()
{
// Complex c1;
// Complex *ptr=&c1;
Complex *ptr = new Complex;
// (*ptr).setData(10, 20);
ptr->setData(10, 20);
// (*ptr).getData();
ptr->getData();
Complex *ptr1 = new Complex[4];
ptr1->setData(1, 4);
ptr1->getData();
return 0;
}
#include <iostream>
#include <iomanip>
using namespace std;
class complex
{
int a, b;
public:
complex(int x, int y)
{
a = x;
b = y;
}
void printNumber()
{
cout << "Your number is " << a << " + " << b << " i " <<
endl;
}
~complex() // We cant pass parameter to destructor
{
cout<<"Destructor called"<<endl;
}
};
int main()
{
complex c1(4, 6);
c1.printNumber();
// We can create multiple constructors in public and we can
invoked it easily
return 0;
}
7:-> Given that an EMPLOYEE class contains following members: data
members: Employee number, Employee name, Basic, DA, IT, Net Salary
and print data members.
#include <windows.h>
#include <iostream>
class employee
{
int emp_number;
char emp_name[20];
float emp_basic;
float emp_da;
float emp_it;
float emp_net_sal;
public:
void get_emp_details();
float find_net_salary(float basic, float da, float it);
void show_emp_details();
};
void employee :: get_emp_details()
{
cout<<"\nEnter employee number: ";
cin>>emp_number;
cout<<"\nEnter employee name: ";
cin>>emp_name;
cout<<"\nEnter employee basic: ";
cin>>emp_basic;
cout<<"\nEnter employee DA: ";
cin>>emp_da;
cout<<"\nEnter employee IT: ";
cin>>emp_it;
}
int main()
{
employee emp;
emp.get_emp_details();
emp.show_emp_details();
return 0;
}
8:->Write a program to illustrate the concept of console I/O
operations.
#include <iostream>
using namespace std;
int main ()
{
int i;
cout << "Please enter an integer value: ";
cin >> i;
cout << "The value you entered is " << i;
cout << " and its double is " << i*2 << ".\n";
return 0;
}
int main()
{
int a = 10;
return 0;
}
public:
void set_roll_number(int);
void get_roll_number(void);
};
void Student ::set_roll_number(int r)
{
roll_number = r;
}
void Student ::get_roll_number()
{
cout << "The roll number is " << roll_number << endl;
}
class Exam : public Student
{
protected:
float maths;
float physics;
public:
void set_marks(float, float);
void get_marks(void);
};
void Exam::set_marks(float m1,float m2){
maths=m1;
physics=m2;
}
void Exam :: get_marks(){
cout<<"The marks obtained in maths are: "<<maths<<endl;
cout<<"The marks obtained in physics are: "<<physics<<endl;
}
class Result : public Exam{
float percentage;
public:
void display_results(){
get_roll_number();
get_marks();
cout<<"Your percentage is"<<(maths+physics)/2<<endl;
}
};
int main()
{
Result harry;
harry.set_roll_number(420);
harry.set_marks(94.0,99.0);
harry.display_results();
return 0;
}
public:
void setData(int a, float b)
{
id = a;
price = b;
}
void getData(void)
{
cout << "Code of this item is " << id << endl;
cout << "Price of this item is " << price << endl;
}
};
int main()
{
int size = 2;
// int *ptr = &size;
// int *ptr = new int[34];
ShopItem *ptr = new ShopItem[size];
ShopItem *ptrTemp=ptr;
int id;
float price;
for (int i = 0; i < size; i++)
{
cout << "Enter ID and price of item " << i + 1<<endl;
cin >> id >> price;
// (*ptr).setData(id,price);
ptr->setData(id, price);
ptr++;
}
for (int j = 0; j < size; j++)
{
cout<<"Item number "<<j+1<<endl;
ptrTemp->getData();
ptrTemp++;
}
return 0;
}
int main() {
TestClass tc;
tc.Display();
cout<<endl;
--tc;
tc.Display();
return 0;
}
15:->Write a program to create ,write and read using fstream header file.
#include<iostream>
#include<fstream>
int main(){
ofstream hout("sample60.txt");
string name;
cin>>name;
hout.close();
ifstream hin("sample60.txt");
string content;
hin>>content;
cout<<"The content of this file is: "<<content;
getline(hin,content,'$');
cout<<content;
return 0;
#include<iostream>
class class2;
class class1
protected:
int num1;
public:
class1()
num1=10;
void show()
class class2
protected:
int num2;
public:
class2()
num2=20;
void show()
};
int no3;
no3=no1->num1;
no1->num1=no2->num2;
no2->num2=no3;
int main()
class1 b;
class2 d;
swap(&b, &d);
b.show();
d.show();
return 0;
}