INTERNAL ASSESSMENT Scheme and Solution -T2 Date : 30/3/2015 Max Marks : 50 Subject & Code : Object Oriented Programming with C++(13MCA22 ) Name of faculty : R.Jayanthi Time : 11.30 am -1.00 Pm Answer any 5 questions 1 a Explain dynamic memory allocation and De-allocation of arrays with an example 7 In many situations we don t know how much memory we need until runtime In that situation we define an array sized to hold the largest string but this wastes memory C++ provides a different approach to obtain blocks of memory The new operator which obtains memory from the operating system And returns a pointer to its starting point. #include<iostream.h> #include<cstring> Int main() char* str= Welcome to c++ programming ; Int len = strlen(str); Char*ptr; Ptr= new char[len+1]; Strcpy(ptr,str); Cout<< ptr= <<ptr<<endl; Delete[]ptr; Return 0; The expression ptr= new char[len+1] Returns a pointer to a section of memory just large enough to hold the string str, Whose length len we found with string library function,plus an extra byte for null character \0 at the end of the string. New is similar to malloc() New-returns a pointer to the appropriate data type Malloc()-pointer must be cast to the appropriate data type. Delete[]ptr-returns memory to the operating system whatever memory was pointed to by ptr. The brackets following delete indicates that we are deleting an array.if you create a single Object with new for eg. Ptr =new someclass;
Delete ptr; In many situations we don t know how much memory we need until runtime In that situation we define an array sized to hold the largest string but this wastes memory C++ provides a different approach to obtain blocks of memory The new operator which obtains memory from the operating system And returns a pointer to its starting point. #include<iostream.h> #include<cstring> Int main() char* str= Welcome to c++ programming ; Int len = strlen(str); Char*ptr; Ptr= new char[len+1]; Strcpy(ptr,str); Cout<< ptr= <<ptr<<endl; Delete[]ptr; Return 0; The expression ptr= new char[len+1] Returns a pointer to a section of memory just large enough to hold the string str, Whose length len we found with string library function,plus an extra byte for null character \0 at the end of the string. New is similar to malloc() New-returns a pointer to the appropriate data type Malloc()-pointer must be cast to the appropriate data type. Delete[]ptr-returns memory to the operating system whatever memory was pointed to by ptr. The brackets following delete indicates that we are deleting an array.if you create a single Object with new for eg. Ptr =new someclass; Delete ptr; b What are abstract classes. Explain their use in C++ 3 The classes which are used to derive new class but out of which we cannot create any objects of its own are called abstract classes class A
; Class B:public c ; Main() A obja //A is abstract class and we cannot create objects of type A 2 a Explain about pointers to class members.what is the advantage of using array of 3 pointers to objects? Array of pointers to objects allows easy access to a group of objects, and is more flexible than placing the objects themselves in an array In this eg. Pgm. We will see how a group of objects can be sorted by sorting an array of pointers to them,rather than sorting the objects themselves Each elements of array persptr is specified in array notation to be persptr[j].(by using pointer notation) *(persptr +j) To access a member of an object using pointer, we use -> operator Persptr[j]->getname() This executes the getname() fn. In the person object pointed to by the element j of the persptr array. b Write a C++ program that uses array of pointers to objects 7 class person Protected: Char name[40]; Public: Void setname() cout<< enter name ; Cin>>name; Void printname() cout<< name is <<name; ; Int main() Person* persptr[100]; Int n=0,char choice;
Do Persptr[n]= new person; Persptr[n]->setname(); n++; Cout<< enter another(y/n)? ; Cin>> choice; While(choice == y ); For(int j=0;j<n;j++) Cout<< person no. <<j+1; Persptr[j]->printname(); Return 0; 3 Write a program in C++ to sort n numbers using bubble sort technique 10 #include<iostream> Using namespace std; Int main() void bsort(int*,int); //prototype Const int N =10; int arr[n]=37,84,62,91,11,65,57,28,19,49; Bsort(arr, N); //sort the array For(int j=0;j<n;j++) Cout<<arr[j]<< ; Return 0;
Void bsort(int* ptr,int n) void order(int*, int*); Int j.k; For(j=0;j<n-1;j++) For(k=j+1;k<n;k++) Order(ptr+j, ptr+k); Void order(int* numb1,int* numb2) //orders two numbers if(*numb1 > *numb2) int temp=*numb1 *numb1=*numb2; //swap them *numb2= temp; 4 a What is copy constructor? Explain it with an example program 7 a2=a1 // assignment stmt; We can define Alpha a3 = a2; // copy initialization not assignment With the same effect as Alpha a3(a2); // alternate form of copy initialization #include<iostream> using namespace std; Class alpha Private:
Int data; Public: Alpha() Alpha(int d) data =d; Alpha(alpha& a) // copy constructor data = a.data; Cout<< copy constructor invoked ; Voiud display() cout<<data; Void operator =(alpha& a)// overloaded = operator data =a.data; Cout<< assignment operator invoked ; ; Int main() alpha a1(37); Alpha a2; a2=a1; // invoke overloaded = Cout<< a2= ; a2.display(); Alpha a3(a1); // invoke copy constructor // alpha a3 =a1; Cout<< a3= ; a3.display();return 0; Here the copy constructor takes one argument:an object of type alpha,passed by reference. Alpha(alpha&) has the form X(X&)(pronounced X of X ref) b Use of scope resolution operator 3 It permits a program to reference an identifier in the global scope that has been hidden by another identifier with the same name in the local scope. It is used to access an item that is outside the current scope. It is used for distinguishing class members and defining class methods.
A major application of the scope resolution operator is in the classes to identify the class to which a member function belongs. C++The :: (scope resolution) operator is used to qualify hidden names so that you can still use them. You can use the unary scope operator if a namespace scope or global scope name is hidden by an explicit declaration of the same name in a block or class. For example: int count = 0; int main(void) int count = 0; ::count = 1; // set global count to 1 count = 2; // set local count to 2 return 0; The declaration of count declared in the main() function hides the integer named count declared in global namespace scope. The statement ::count = 1 accesses the variable named count declared in global namespace scope. You can also use the class scope operator to qualify class names or class member names. If a class member name is hidden, you can use it by qualifying it with its class name and the class scope operator. 5 What is inheritance? What are the different types of inheritance? Explain how multiple inheritance is implemented in C++ with an example 10 Inheritance is the most powerful feature of object-oriented programming. Inheritance is the process of creating new classes, called derived classes from the existing or base classes. The derived class inherits all the capabilities of the base class but can add refinements of its own. Single inheritance class A ; Class b : public A
; Class b is derived from class A Levels of inheritance class A ; Class b : public A ; Class c: public B ; B is derived from A,C is derived frm B.This process can be extended to any no. of levels Multiple inheritance A class can be derived from more than one base classs.this is called multiple inheritance. Class c is derived from base classes A and B. class A ; Class B ; Class C: public A,public b ;
Example program for multiple inheritance class emp Private: Char name[len]; Public :void getdata) cout<< enter name ;cin >> name; Void putdata() cout<<name; ; Class manager:public emp private: double dues; Void getdata() emp :: getdata(); Cout<< enter dues ;cin>> dues; Void pudata() cout<< dues; ; Class scientist: public emp, private manager private: int pubs; public: Void getdata() Emp:: getdata();
Manager:: getdata(); Cout<< enter pubs ; cin>> pubs; Void putdata() emp:: pudata(); Manager :: putdata(); Cout<<pubs; ; Int main() scientist s1; s1.getdata(); s1.putdata(); Return 0; 6 What is operator overloading explain with a program to concatenate 2 strings by overloading + operator and << operator 10 #include <iostream.h> #include <string.h> #include <conio.h> class string private: char str[20]; public: string(char s[]) strcpy(str, s);
string operator +(string s) strcat(str, s.str); return *this; string (string &p) strcpy(str,p.str); string operator <<(string s) cout<< s.str; return 0; ; void main() clrscr(); string s1 ("VTU"); string s2 = "BELGAUM"; cout<< "String S1:"; s1 << s1<<"\n"; cout<< "String S2:"; s2 <<s2<<"\n"; string s3 = s1 + s2; cout<< "\nstring S1 + S2: "; s3 << s3; getch(); 7 a Define friend functions and friend classes in C++ and discuss Why are they used? 3 The concept of data hiding and encapsulation dictate that nonmember fns should not be able to access an objects private or protected data
Imagine that u want a fn to operate on objects of 2 different classes. The fn. Will take objects of the 2 classes as arguments, and operate on their private data.here the friend fn. Can act as a bridge between 2 classes Friend fn. Will take the objects of 2 different classes as its argument The member functions of a class can all be made friends at the same time when you make the entire class a friend b Write a C++ program using Friend function that uses objects of 2 different classes as function arguments 7 Class beta; Class alpha private: Int data; Public: Alpha():data(3) //no-arg constructor Friend int frifunc(alpha,beta); //friend fn ; Class beta Private: Int data; Public: beta():data(7) //no-arg constructor Friend int frifunc(alpha,beta); //friend fn ; Int frifunc(alpha a, beta b) // fn. definition return (a.data + b.data) Int main() Alpha aa; Beta bb; Cout<< frifunc(aa,bb)<<endl; Return 0;
8 Demonstrate the use of static member function and static members of a class with an example program 10 Static data member is not duplicated for each object;rather a single item is shared by all objects of a class. Lets see how functions as well as data may be static. #include<iostream> using name space std; Class gamma Private: Static int total; Int id; Public: Gamma() total ++; Id= total; ~gamma() total --; Cout<< destroying id no <<id<<endl; Static void showtotal() Cout<< total is << total;
Void showid() cout<< id no is <<id ; int main() gamma g1; Gamma :: showtotal(); Gamma g2,g3; Gamma :: showtotal(); G1.showid(); G2.showid(); G3.showid(); Return 0; PESIT, BSC