Data and Member functions in oop(object oriented programming)
Data and Member functions
Task 1:
Write
a program to create class named employee.
Data Members:
Emp_id
Emp_name
Emp_age
Emp_email
Emp_salary
Member Functions:
void
getinfo(int,string,int,int,string,int);
void displayinfo();
#include <iostream>
#include<string>
using namespace std;
class employee{
private:
int id;
string name;
int age;
string email;
int salary;
public:
void getdata(int a, string x, int b, string y, int c)
{
id = a; name = x; age = b; email = y; salary = c;
}
void display()
{
cout << "Id is:" << id << endl;
cout << "Age is:" << age << endl;
cout << "Name is:" <<name<< endl;
cout << "Email is:" << email << endl;
cout << "Salary is:" << salary << endl;
}
};
int main()
{
employee e;
int a, b, c;
string x, y;
cout << "Enter the id:";
cin >> a;
cin.ignore();
cout << "Enter the name:";
getline(cin, x);
cout << "Enter the age:";
cin >> b;
cout << "Enter the email:";
cin.ignore();
getline(cin, y);
cout << "Enter the salary:";
cin >> c;
e.getdata(a,x,b,y,c);
e.display();
getchar();
getchar();
return 0;
}
Task 2:
Perform Task 1 using scope resolution operator and pointer object.
#include <iostream>
#include<string>
using namespace std;
class employee{
private:
int id;
string name;
int age;
string email;
int salary;
public:
void getdata(int, string, int, string, int);
void display();
};
int main()
{
employee e;
int a, b, c;
string x, y;
cout << "Enter the id:";
cin >> a;
cin.ignore();
cout << "Enter the name:";
getline(cin, x);
cout << "Enter the age:";
cin >> b;
cout << "Enter the email:";
cin.ignore();
getline(cin, y);
cout << "Enter the salary:";
cin >> c;
e.getdata(a,x,b,y,c);
e.display();
getchar();
getchar();
return 0;
}
void employee::getdata(int a, string x, int b, string y, int c)
{
id = a; name = x; age = b; email = y; salary = c;
}
void employee::display()
{
{
cout << "Id is:" << id << endl;
cout << "Age is:" << age << endl;
cout << "Name is:" << name << endl;
cout << "Email is:" << email << endl;
cout << "Salary is:" << salary << endl;
}
}
Task 3:
Define
a Class BOOK with the following specifications:
Data Members:
bookNo
bookTitle
price
Member Functions:
total_cost(
) A function to calculate the total cost for N no of copies where N is
passed
to function as argument.
take_data(
) A function to input bookNo, bookTitle, price.
purchase_info(
) A function to ask the user to input the number of copies to be
purchased.
It invokes total_cost( ) and prints the total cost paid by
the user.
#include<iostream>
#include<string>
using namespace std;
class book{
int no;
string title;
int price;
public:
void total_cost(int);
void take_data(int);
void purchase_info();
};
int main()
{
book b1;
int a = 300;
b1.take_data(a);
b1.purchase_info();
getchar();
getchar();
return 0;
}
void book::total_cost(int n)
{
cout << n*price << endl;
}
void book::take_data(int i)
{
price = i;
}
void book::purchase_info()
{
int no;
cout << "Enter the number of copies:";
cin >> no;
cout << "Enter the title of the book:";
cin.ignore();
getline(cin, title);
cout << "Total cost is:";
total_cost(no);
}
Comments
Post a Comment