Assignment No-4 Subject: Cse-202: Object Oriented Programming

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 9

Assignment no-4

Subject: cse-202
Object oriented programming

Submitted to: Submitted by:


Mr.Hargit Singh Rahul Arora
Roll No:- A03
Section:H6901
Q.1 Does an abstract need a constructor? Why?

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.

Q.2 Write a program in C++ which read a set of character using


pointer then print in reverse order.

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;

Q. 3) Write a program which store marks of a class students, read


the number of
students, from user. Use Dynamic Array.

Ans:

#include <iostream.h>

#include<conio.h>

void main()

// an array of double to store student's score

double studentscore[50];

// a 2D array of string to store student's name

char studentname[50][50];
double studentavg = 0.0, sumscore = 0.0, averagescore = 0.0, highestscore =0.0;

// index and terminal variables

int i = 0, stop = 0, k = 0;

// read and store student name and score

do

Cout<<"Enter student name: "<<endl;

// null terminated string, cin>> only accept 1 string

// can try gets()/gets_s()

Cin>>studentname;

Cout<<"Enter student score: "<<endl;

Cin>>studentscore;

// increment the array index

i++;

// continue for next data?

Cout<<"More data? -1 to stop, others to continue: "<<endl;

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

// and then compare 1 by 1 in the for loop...

highestscore = studentscore[0];

// the i index less 1, coz we increment after storing it

// in the do-while loop...

for(k=0;k<=i-1;k++)

// display all the student names and their respective scores

Cin>>t>>studentname>>studentscore;

// summing up all the score for average calculation

sumscore = sumscore + studentscore;

// determining the highest score

if(highestscore < studentscore)

highestscore = studentscore;

// calculate class average score

Cout<<”\nThe number of student is”<<i;

averagescore = sumscore / i;

cout<<"The average score for this class is”<< averagescore;

// some cosmetic formatting...


Coout<<"\n================================================\n";

Cout<<"Below The Average Students! Work Harder!\n";

Cout<<"================================================\n";

cout"\nStudent Name ,,,, Score\n";

cout<<"------------,,,,-----\n";

// list down all the below average students

for(k=0;k<=i-1;k++)

if(studentscore < averagescore)

cout<<”studentname, studentscore”;

// some cosmetic formatting...

Cout<<"\n================================================\n";

Cout<<"Top Scorer Student! Congratulation!\n";

Cout<<"================================================\n”;

Cout<<"Student Name…….Score\n";

Cout<<"-----------…..-----\n";

// list down all the highest mark students

for(k=0;k<=i-1;k++)

{
if(studentscore == highestscore)

cout<<”studentname….. studentscore;

return 0;

Q.4) Discuss and explain the memory management operators in


C++ with the help of a programming example.

Ans:4 There are two types of memory management operators in C++:

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.

The general syntax of new operator in C++ is as follows:

(pointer variable = new datatype; )

For example:

int *a=new int;

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 assignment can be made in either of the two ways:

int *a = new int;


*a = 20;
or
int *a = new int(20);
2. Delete operator:

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.

The general syntax of delete operator in C++ is as follows:

(delete pointer variable; )

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;

Q.6) How do the following two statements differ in operation?


cin>>c;
cin.get(c);

Ans :

Here are two possible solutions that define why How do the following two statements differ in
operation?
cin>>c;
cin.get(c);

declare first and last to be quite large:


char first[256], last[256];
cin.getline(first, sizeof(first));
cin.getline(last, sizeof(last));

Or, in the Standard Template Library (STL) there is a string class that helps with this sitution a
lot:

string first, last;

getline(cin,first);
getline(cin, last);

You might also like