PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of MCA

Similar documents
Darshan Institute of Engineering & Technology for Diploma Studies

Object Oriented Pragramming (22316)

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year

Data type of a pointer must be same as the data type of the variable to which the pointer variable is pointing. Here are a few examples:

Pointers. Developed By Ms. K.M.Sanghavi

PES Institute of Technology


2. Distinguish between a unary, a binary and a ternary operator. Give examples of C++ operators for each one of them.

cout<< \n Enter values for a and b... ; cin>>a>>b;

OOP THROUGH C++(R16) int *x; float *f; char *c;

DE122/DC106 Object Oriented Programming with C++ DEC 2014

Data Structures using OOP C++ Lecture 3

C++ 8. Constructors and Destructors

THE NAME OF THE CONSTRUCTOR AND DESTRUCTOR(HAVING (~) BEFORE ITS NAME) FUNCTION MUST BE SAME AS THE NAME OF THE CLASS IN WHICH THEY ARE DECLARED.

Pointers, Dynamic Data, and Reference Types

Fast Introduction to Object Oriented Programming and C++

Recharge (int, int, int); //constructor declared void disply();

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!

Introduction to Programming Using Java (98-388)

Short Notes of CS201

Object Oriented Programming. Solved MCQs - Part 2

IBS Technical Interview Questions

CS201 - Introduction to Programming Glossary By

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Bangalore South Campus

KOM3191 Object Oriented Programming Dr Muharrem Mercimek OPERATOR OVERLOADING. KOM3191 Object-Oriented Programming


AN OVERVIEW OF C++ 1

PROGRAMMING IN C++ COURSE CONTENT

EL2310 Scientific Programming

Instructions. Quiz #2. Name: Solutions Student Number: Signature:

Object Oriented Programming(OOP).

W3101: Programming Languages C++ Ramana Isukapalli

STRUCTURING OF PROGRAM

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT).

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

Name: Username: I. 20. Section: II. p p p III. p p p p Total 100. CMSC 202 Section 06 Fall 2015

Chapter-11 POINTERS. Important 3 Marks. Introduction: Memory Utilization of Pointer: Pointer:

Lecture 8: Object-Oriented Programming (OOP) EE3490E: Programming S1 2017/2018 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology

Page. No. 1/15 CS201 Introduction to Programmming Solved Subjective Questions From spring 2010 Final Term Papers By vuzs Team

CHAPTER 4 FUNCTIONS. 4.1 Introduction

Review Questions for Final Exam

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2015 EOM, HYEONSANG ALL RIGHTS RESERVED

PESIT Bangalore South Campus

OBJECT ORIENTED PROGRAMMING. Ms. Ajeta Nandal C.R.Polytechnic,Rohtak

SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR

Pointers. Pointers. Pointer Variables. Pointers

Introduction to C++ Systems Programming

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING REWRAP TEST I CS6301 PROGRAMMING DATA STRUCTURES II

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

CMSC 202 Midterm Exam 1 Fall 2015

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

IBS Software Services Technical Interview Questions. Q1. What is the difference between declaration and definition?

Cpt S 122 Data Structures. Introduction to C++ Part II

Sample Paper 2013 SUB: COMPUTER SCIENCE GRADE XII TIME: 3 Hrs Marks: 70

CS201 Some Important Definitions

Announcements. Lecture 04b Header Classes. Review (again) Comments on PA1 & PA2. Warning about Arrays. Arrays 9/15/17

Chapter 1: Object-Oriented Programming Using C++

Subject: Fundamental of Computer Programming 2068

Solution: A pointer is a variable that holds the address of another object (data item) rather than a value.

C++ for Java Programmers

Instantiation of Template class

C++ For Science and Engineering Lecture 15

Padasalai.Net s Model Question Paper

CS349/SE382 A1 C Programming Tutorial

Programming in C++: Assignment Week 2

Module Operator Overloading and Type Conversion. Table of Contents

The Address-of Operator &: The & operator can find address occupied by a variable. If var is a variable then, &vargives the address of that variable.

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

COMS W3101 Programming Language: C++ (Fall 2016) Ramana Isukapalli

Week 8: Operator overloading

C++ Scope Resolution Operator ::

Structured Data. CIS 15 : Spring 2007

Reference Parameters A reference parameter is an alias for its corresponding argument in the function call. Use the ampersand (&) to indicate that

Object oriented programming

CONSTRUCTORS AND DESTRUCTORS

COMP322 - Introduction to C++

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING OBJECT ORIENTED PROGRAMMING CLASS : THIRD SEMESTER CSE

COMP 2355 Introduction to Systems Programming

CSC1322 Object-Oriented Programming Concepts

+2 Volume II OBJECT TECHNOLOGY OBJECTIVE QUESTIONS R.Sreenivasan SanThome HSS, Chennai-4. Chapter -1

OBJECT ORIENTED PROGRAMMING

Classes and Objects. Class scope: - private members are only accessible by the class methods.

KOM3191 Object Oriented Programming Dr Muharrem Mercimek ARRAYS ~ VECTORS. KOM3191 Object-Oriented Computer Programming

Quiz Start Time: 09:34 PM Time Left 82 sec(s)

CS201 Latest Solved MCQs

Functions. Introduction :

EAS 230 Fall 2002 Section B

Introduction to C++ Introduction to C++ 1

MAHALAKSHMI ENGINEERING COLLEGE B TIRUCHIRAPALLI

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book.

Interview Questions of C++

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

Introduction to C++ Friends, Nesting, Static Members, and Templates Topic #7

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

G52CPP C++ Programming Lecture 13

JAYARAM COLLEGE OF ENGINEERING AND TECHNOLOGY Pagalavadi, Tiruchirappalli (An approved by AICTE and Affiliated to Anna University)

CSC 211 Intermediate Programming. Arrays & Pointers

Constructor - example

Transcription:

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