0% found this document useful (0 votes)
10 views

AssignmentmeoopT

Uploaded by

shehzaibmirza428
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

AssignmentmeoopT

Uploaded by

shehzaibmirza428
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

OOP -

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;

cout<<"\nenter price of book=";


cin>>price;

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

1. Initialization: Ensures that objects are properly initialized with


default or provided values.
2. Memory Allocation: Allocates memory for the object and its
members.
3. Object Creation: Called automatically when an object is created.
4. Parameterized Construction: Allows objects to be initialized with
varying parameters.

Destructor:

A destructor is a special member function that cleans up resources


when an object is destroyed. Its importance includes:

1. Memory Deallocation: Releases memory allocated by the


constructor.
2. Resource Cleanup: Closes files, network connections, or other
resources used by the object.
3. Preventing Memory Leaks: Ensures that memory is released to
prevent memory leaks.
4. Automatic Invocation: Called automatically when an object
goes out of scope.

You might also like