AssignmentmeoopT
AssignmentmeoopT
Assignment
Theory
.
SHEHZAIB
S2024332016
Question 1:
#include <iostream>
using namespace std;
class book{
public:
string title;
string author;
int price;
book(string t = "", string a = "", int p = 0)
{
title = t;
author = a;
price = p;
}
void getData()
{ cout<<"enter title of
book="; cin>>title;
cout<<"\nenter author book=";
cin>>author;
}
void display(){
cout<<"title="<<title<<endl;
cout<<"author="<<author<<endl;
cout<<"price="<<price<<endl;
}
};
int main()
{ int n;
cout<<"enter number of books you want to store info for=";
cin>>n;
book* books = new book[n];
for(int i=0;i<n;i++){
cout<<"enter data for book no "<<i+1<<endl;
books[i].getdata();
}
for(int i=0;i<n;i++){
cout<<"info for book no "<<i+1<<" is:"<<endl;
books[i].display();
}
int totalPrice = 0; for
(int i = 0; i < n; ++i)
{ totalPrice +=
books[i].price;
}
cout << "Total price of all books: $" << totalPrice << endl;
delete[] books; return 0;
}
Output:
Question 2:
#include <iostream>
#include<string> using
namespace std; class
student{ string
name; int age;
double grade; public:
student()
{ name="Unknown
";
age=0;
grade=0.0;
}
student(string n,int a,double g){
name=n; age=a;
grade=g;
}
student(student & st3)
{ name=st3.name;
age=st3.age;
grade=st3.grade;
}
void setdata(){ cout<<"enter
name of student="; cin>>name;
cout<<"enter age of student=";
cin>>age;
cout<<"enter grade of student=";
cin>>grade;
}
void getdata()
{ cout<<"name="<<name<<endl;
cout<<"age="<<age<<endl;
cout<<"grade="<<grade<<endl;
}
void scholarship()
{ if(grade>=85){
cout<<"eligible for scholorship"<<endl;
}
else{
cout<<"not eligible for scholorship"<<endl;
}
}
};
int main() {
student st1,st2("shazaib",55,66),st3(st2);
st1.setdata(); cout<<"data for student
1:"<<endl; st1.getdata(); st1.scholarship();
cout<<"data for student 2:"<<endl;
st2.getdata(); st2.scholarship(); cout<<"data
for student 3:"<<endl; st3.getdata();
st3.scholarship();
return 0;
}
Output:
Discuss the importance of constructor and destructor:
Constructor:
A constructor is a special member function that initializes an object
when it is created. Its importance includes:
Destructor: