Assignment No-4 Subject: Cse-202: Object Oriented Programming
Assignment No-4 Subject: Cse-202: Object Oriented Programming
Assignment No-4 Subject: Cse-202: Object Oriented Programming
Subject: cse-202
Object oriented programming
Ans:
Because abstract classes should never be instantiated, it is important to correctly define their
constructors. It is also important to ensure that the functionality of your abstract class is correct
and easily extended.
If we define a constructor in an abstract class, the base class can perform initialization tasks when
instances of a derived class are created. An internal constructor prevents the abstract class from
being used as the base class of types that are not in the same assembly as the abstract class. In the
abstract class design. It also means that for high-level scenarios where developers might not
understand abstract classes and inheritance, they can use the concrete class without having to
learn these concepts.
Ans:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int *ptr,i,n;
clrscr();
cout<<"Enter the no of elements:”<<endl;
cin>>n;
ptr=(int *)malloc(sizeof(int)*n);
if(ptr==NULL)
{
cout<<"Not enough memory”<<endl;
exit(1);
}
for(i=0; i<n; i++)
{
cout<<"Enter %d element : ",i+1);
Cin>>ptr;
}
cout<<"Array in original order\n”<<endl;
for(i=0; i<n; i++)
{
cout<<ptr;
}
cout<<"Array in reverse order\n”<<endl;
for(i=n-1; i>=0; i--)
{
cout<<ptr;
}
getch();
return 0;
Ans:
#include <iostream.h>
#include<conio.h>
void main()
double studentscore[50];
char studentname[50][50];
double studentavg = 0.0, sumscore = 0.0, averagescore = 0.0, highestscore =0.0;
int i = 0, stop = 0, k = 0;
do
Cin>>studentname;
Cin>>studentscore;
i++;
Cin>>stop;
while(stop != -1);
// some cosmetic...
Cout<<"\n=================REPORT====================\n";
Cout<<"Student Name\tScore\n";
Cout<<"------------\t-----\n";
// set initial value of the highest score to the 1st array element
highestscore = studentscore[0];
for(k=0;k<=i-1;k++)
Cin>>t>>studentname>>studentscore;
highestscore = studentscore;
averagescore = sumscore / i;
Cout<<"================================================\n";
cout<<"------------,,,,-----\n";
for(k=0;k<=i-1;k++)
cout<<”studentname, studentscore”;
Cout<<"\n================================================\n";
Cout<<"================================================\n”;
Cout<<"Student Name…….Score\n";
Cout<<"-----------…..-----\n";
for(k=0;k<=i-1;k++)
{
if(studentscore == highestscore)
cout<<”studentname….. studentscore;
return 0;
1. New
2. Delete
New operator:
The new operator in C++ is used for dynamic storage allocation. This operator can be used to
create object of any type.
For example:
the new operator allocates sufficient memory to hold the object of datatype int and returns a
pointer to its starting point. The pointer variable a holds the address of memory space allocated.
The delete operator in C++ is used for releasing memory space when the object is no longer
needed. Once a new operator is used, it is efficient to use the corresponding delete operator for
release of memory.
delete is a keyword and the pointer variable is the pointer that points to the objects already
created in the new operator. Some of the important points the programmer must note while using
memory management operators are described below:
• The programmer must take care not to free or delete a pointer variable that has already been deleted.
.
• Overloading of new and delete operator is possible (to be discussed in detail in later section on
overloading).
.
• We know that sizeof operator is used for computing the size of the object. Using memory management
operator, the size of the object is automatically computed.
.
• The programmer must take care not to free or delete pointer variables that have not been allocated using a
new operator.
.
• Null pointer is returned by the new operator when there is insufficient memory available for allocation.
Example:
#include <iostream.h>
void main()
{
//Allocates using new operator memory space in memory for storing a integer datatype
int *a= new a;
*a=100;
cout << " The Output is:a="<<a;
//Memory Released using delete operator
delete a;
Ans :
Here are two possible solutions that define why How do the following two statements differ in
operation?
cin>>c;
cin.get(c);
Or, in the Standard Template Library (STL) there is a string class that helps with this sitution a
lot:
getline(cin,first);
getline(cin, last);