Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Practical File

Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

1:->Write a program to print a message “Welcome to C++ Programming”

without using class.


#include<iostream>
using namespace std;
int main()
{
cout<<"Welcome to C++ Proramming";
return 0;
}

2:->Write a program to display a “Most Welcome to GNIOT College”


message by using class.
#include<iostream>
using namespace std;
class GNIOT{
public:
void display(){
cout<<"Most Welcome to GNIOT College";
}
};
int main()
{
GNIOT gn;
gn.display();
return 0;
}
3:->Write a Program to display Names,Roll No,class name and grades
of students input by user.
#include <iostream>
using namespace std;
class Student
{
string Name;
int Roll_Number;
string Class_Name;
int marks;
// char Grades;
public:
void ReadDetails();
void GradeCalculate();
void PrintDetail();
};
void Student ::ReadDetails()
{
cout << "Enter the name of student:->";
cin >> Name;
cout << "\n Enter the roll number:->";
cin >> Roll_Number;
cout << "\n Enter the class name:->";
cin >> Class_Name;
cout << "Enter total marks out of 500:->";
cin >> marks;
}
void Student ::GradeCalculate()
{
float percentage;
percentage = (float)marks / 500 * 100;
cout << percentage << "%\n";
if (percentage >= 75)
{
cout << "Grade :-> A\n";
}
else if ((percentage >= 60.00) && (percentage < 75.0))
{
cout << "Grade :-> B\n";
}
else if ((percentage >= 40.00) && (percentage < 60.0))
{
cout << "Grade :-> C\n";
}
else
{
cout << "Grade :-> D\n";
}
}
void Student ::PrintDetail()
{
cout << "Student Details:\n";
cout << "Name: " << Name << endl;
cout << "Roll Number: " << Roll_Number << endl;
cout << "Class Name: " << Class_Name << endl;
cout << "Total Marks obtained:->" << marks << " out of 500" <<
endl;
}
int main()

{
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>

using namespace std;

struct college_info
{
char college_name[15];
char college_code[2];
char dept[50];
int intake;
};

int main()
{
struct college_info college;

cout << "\n+++++ Enter the College Information +++++\n\n";


cout << "Name of the college: ";
cin >> college.college_name;
cout << "College Code: ";
cin >> college.college_code;
cout << "Department: ";
cin >> college.dept;
cout << "Department In-take: ";
cin >> college.intake;

cout << "\n\n********* College Information *********\n\n";


cout << "Name of the college : " << college.college_name;
cout << "\nCollege University Code: " << college.college_code;
cout << "\nName of the Department: " << college.dept;
cout << "\nThe department of " << college.dept << " has in-take
: " << college.intake;
cout << "\n\n-----------------------------------------\n\n";

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;
}

6:->Write a program of Parameterised constructor and destructor.

#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>

using namespace std;

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;
}

float employee :: find_net_salary(float basic, float da, float it)


{
return (basic+da)-it;
}

void employee :: show_emp_details()


{
cout<<"\n\n**** Details of Employee ****";
cout<<"\nEmployee Name : "<<emp_name;
cout<<"\nEmployee number : "<<emp_number;
cout<<"\nBasic salary : "<<emp_basic;
cout<<"\nEmployee DA : "<<emp_da;
cout<<"\nIncome Tax : "<<emp_it;
cout<<"\nNet Salary : "<<find_net_salary(emp_basic,
emp_da, emp_it);
cout<<"\n-------------------------------\n\n";
}

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;
}

9:->Write a program to use scope resolution operator.Display the


various values of the same variable declared at different scope
levels.
#include <iostream>
using namespace std;
int a = 20;

int main()
{
int a = 10;

cout << "Value of local a: " << a << endl;


//use of SCOPE RESOLUTION OPERATOR (::) to access global
variable.
cout << "Value of global a: " << ::a << endl;

return 0;
}

10:->Write a program to allocate memory using new operator.


#include<iostream>
#include<iomanip>
using namespace std;
int main(){
int a=4;
int *ptr=&a;
cout<<"The Value of a is "<<*(ptr)<<endl;
int *p=new int(40);
cout<<"The value at address p is "<<*(p)<<endl;
int *arr=new int[3];
arr[0]=10;
*(arr+1)=20;
arr[2]=30;
// delete[]arr;
cout<<"The value of arr[0] is "<<arr[0]<<endl;
cout<<"The value of arr[1] is "<<arr[1]<<endl;
cout<<"The value of arr[2] is "<<arr[2]<<endl;
return 0;
}

11:->Write a program to create multilevel inheritance.


#include <iostream>
#include <iomanip>
using namespace std;
class Student
{
protected:
int roll_number;

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;
}

12:->Write a program to create an array of pointer.Invoke functions


using array objects.
#include <iostream>
#include <iomanip>
using namespace std;
class ShopItem
{
int id;
float price;

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;
}

13:->Write a program to create operator overloading in polymorphism.


#include <iostream>
using namespace std;
class TestClass {
private:
int count;
public:
TestClass() : count(5) {}
void operator --() {
count = count - 3;
}
void Display() {
cout << "Count: " << count; }
};

int main() {
TestClass tc;

tc.Display();
cout<<endl;
--tc;
tc.Display();
return 0;
}

14:->Write a program to print multi data value using single data


type.
#include <iostream>
using namespace std;
template <class T1, class T2>
class myClass
{
public:
T1 data1;
T2 data2;
myClass(T1 a, T2 b)
{
data1 = a;
data2 = b;
}
void display()
{
cout << this->data1 << endl
<< this->data2;
}
};
int main()
{myClass<int, char> obj(1, 'c');
obj.display(); }

15:->Write a program to create ,write and read using fstream header file.

#include<iostream>

#include<fstream>

using namespace std;

int main(){

ofstream hout("sample60.txt");

cout<<"Enter your name";

string name;

cin>>name;

hout<<name + " is my 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;

16:->Write a program to swap private data members of classes named as


class1,class2 using friend function.

#include<iostream>

using namespace std;

class class2;

class class1

protected:

int num1;

public:

class1()

num1=10;

void show()

cout<<"\n Value of Number 1 : "<<num1;

friend void swap(class1 *num1, class2 *num2);


};

class class2

protected:

int num2;

public:

class2()

num2=20;

void show()

cout<<"\n Value of Number 2 : "<<num2;

friend void swap(class1 *num1, class2 *num2);

};

void swap(class1 *no1, class2 *no2)

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;
}

You might also like