0% found this document useful (0 votes)
18 views19 pages

OOPs (Unit-2)

BCA 3rd Sem OOPs unit 2 notes

Uploaded by

panther2020.com
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
18 views19 pages

OOPs (Unit-2)

BCA 3rd Sem OOPs unit 2 notes

Uploaded by

panther2020.com
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 19
ee eee UN Q 1. How can achieve Information Hiding in C++. Ans. es | Information Hiding “Information hiding is manifested by enabling programmers to restrict access to certain data and code. This way, a class may prevent other classes and functions from accessing its data members and some of its member functions, while allowing them to access its non-restricted members. As most object-oriented programmers know, information hiding is one of the most important things in OOP. Information hiding is something you do, generally to classes (objects) that protect them from tampering from the user. Instead of the client having access to all of the information you design the interface and methods to allow them to interact on its own terms. 7 “Information hiding example: class clock { private: int ticks; public: void display(); } Explanation: the variable ticks is being hidden from the client in this instance. Q2. What are abstract data types in C++. Ans. Abstract data type (ADT) is a specification of a set of data and the set of operations that can be performed on the data. Such a data type is abstract in the sense that it is independent of various conc: * implementations. The definition can be mathematical, or it can be- programmed as an interface. A first class ADT supports the creation of multiple instances of the ADT, and the interface normally provides 4 constructor, which returns an abstract handle to new data, and several operations, which are functions accepting the abstract handle as an argument.. An ADT is a collection of data and associated operations fo, manipulating that data. ADTs support abstraction, encapsulation. and information hiding they provide equal attention to data and operations 7 Common examples of ADTs: Built-in types: boolean, integer. real, array User-defined types: stack, queue, tree, list boolean: , Values: true and false Operations: and, or, not, nand, ete integer: Values: Whole numbers between MIN and MAX values ~ Operations: add, subtract. multiply, divide, etc. arrays: - Values: + O Q3. What is objects and classess? Describe the systex for declaring a class and object with examples. - “A _- C++ Objects and Classes ee object-oriented programming languages like C++, the data and functions (procedures to manipulate the data) are bundled together as a self-contained unit called an object,A class is an extended concept similar to that of structure in C programming language; this class describes the data. properties alone{ In C++ programming language, class describes both the properties (data) and behaviors (functions) of objects. Classes are nof objects, but they are used to instantiate objects. Features of Class: 3 Classes contain data known as members and member functions. Asa unit, the collection of members.atid member functions is an object. Therefore. this unit of objects makes up a class. How to write a Clas {n Structure in C programming language, a structure is specified with a name. The C++ programming language extends this concept. A class is specified with a name after the keyword class. The starting flower brace symbol * {‘is placed at the beginning of the code. Following the flower brace symbol, the body of the class is defined with the member functions data. Then the class is closed with a flower brace symbol *}* and concluded with a colon *;” jomogencous elements, i.e. array ofX. . . ‘ations: initialize, store, retrieve, copy, etc. (23) Example: class exforsys { data: member_functions; There are different access specifiers for defining the data and functions present inside a class. Access specifiers: Access specifiers are used to identify access rights for the data and member functions of the class. There are three main types of access specifiers in C++ programming language: © private © public © protected © A private member within a class denotes that only members. of the same class have accessibility. The private member is inaccessible from outside the class. © Public members are accessible from outside the class © A protected access specifier is a stage between private and public access. If member functions defined in a class are protected, they cannot be accessed from outside the class but can be accessed from the derived class. When defining access specifiers, the programmer must use the keywords: private, public or protected when needed, followed by a semicolon and then define the data and member functions under it. class exforsys private: ” (24) In the code above, the member x and y are defined as private access, The member function sum is defined as a public access. General Template of a class: General structure for defining a class is: class classname { access_specifier: data_member; member_functions; access_specifier: data_member; member_functions; } : Generally, in class, all members (data) would be declared as private and the member functions would be declared as public. Private is the default access levél-If no access specifiers are identified for members of a class, thé members are defaulted to private access. class exforsys t ; int x,y; public: . void sum() : > i this example, for members x and y of the class exforsys there are no access specifiers identified. exforsys would have the default access specifier as private. ‘reation of Objects: . Once the class is created, one or more objects can be created from he class as objects are instance of the class. jects are also declared as: lass_name followed_by object_name; imple: forsys cl; ; (25) co declares e1 to be an object of class exforsys For example a complete class and object dec! foniteal Seeesicrys ject declaration is given below: { private: int x,y: public: void sum() { hia hs void main() exforsys e1; } . Q 4. Define the terms State, identity and behavior of objects in | > cH? Ans. » Object: e An object has state, behavior, and identity; the structure and behavior of similar objects are defined in their common class.” Some things are not objects, but are attributes; e.g. Age, color. _ Attributes may be properties of some object. ite: ‘he state of an object consists of aset of data fields (its properties) ith their current values. The state represents tl behavior. ent characteristic, trait, or quality that roperty is an inher guishes one kind of object from another (i.e. its class). properties in a class are usually static. perties have some value. he cumulative results of an object’s (26) be a simple quantity. e Eg. a person's net worth is Rs 25000 _, Values are usually dynamic. e Eg a person's net worth may © Occasionally a value is static 2 yeat’s seriar number ; vents (i.e. 4 message eo bt The state of an object changes in response toe’ sent to it), or by acting autonomously. _s May go up OF down. ° Behavior: “Behavior is how of its state changes and message passing.” When you send a message to an obje method (i.e. execute some code). Invoking a method will cause certain well-defined behavior. and may change the object’s state _» The behavior may depend on t A few kinds of operations that a client may per! _, Modifier: alters the state of an object. e Eg. karan.setNetWorth(25000); _, Selector:,-accésses the state of an object, but does not alter it. eo E.g. age = s.getAge(); _+ Constructor: creates an object and initializes its state. _, Destructor: destroys an object (frees its memory). e an object acts and reacts in terms ct, you actually invoke a he object’s current state. form on an object: Identity: . e “Identity is that property of an object, which distinguishes it from all others.” - Ge of a class has its own memory to hold its state. t is Constructor? What are the different types of wi gh wi ait ao prikarm wei g 4 eb a Trike om | ns. Constructor: A constructor is a special member function whose in operation is to allocate the required resources such as memory initialize, the objects of its class. A constructor is distint from Dechieawlon OF CORSIFUCIOr:. eliss abe ' mtn, public abe (void) ; wv yr 1 ee abe»; abe (void) // constructor declaration { n=0; ) she a! // object created « 1 only creates the object al of type abe but also initialize its data member n to zero yok ik wt It has the same name as that of the cJaSs to which it belongs. It is executed automatically whengVer the class is instantiated. l 2. 3. It does not have any return type 4. It is normally used to initialize the data member of a class. 5. It is also used to allocate resources such as memo the dynamic data members of a class. Gon ret be ive Types of Constructor: ngiche an obfut tile ree Mainly constructor are classified into four calggories: a tT. Default Constructor 2. Parameterized Constructor UX Copy Constructor “4. Dynamic Constructor , 1. Default Constructor: Acons called the default constructor. > oreg. # include . # include class abe tructor that accepts no parameter is private : —intas int b; (29) 3. Copy Constructor: A constructor can own class as a parameter, For e.g. # include # include accept a reference to its Class A { int m; intn; public : A(A&); } A (int a, int b) { m=a; n=b; } = A(A&i) { . m=i.m; n=in; } 3 void main () { A 11 (20, 40); AI2(11); getch () ; C3) Q 6. What is destreutors, Explain with example. Ans. Destructor: Destructors is used to destroy the objects that have been created by a constructor, when they are no longer required Syntax: te *~ integer () { \ 5 - e Destructor never takes any argument nor does it return any value. It will be invoked simplicity, by the compiler upon exit from the program. Properties of Destructor: 1. Destructor finally are invoked automatically when the object are destroyed. 2. A destructor may not be static. 3. They can not be inherited. 4. A destructor does not take any value and also does not return "any value. , 5. It isnot possible to take the address of a destructor. 6. No argument is provided to destructor. For e.g. ' # include _ class Test public : Test (); ~ Test (); 2: Test () cout << “Constructor called << Endl ; t Test : :~Test () cout << “destructor called << Endl B void main () { } Testis : i || Constructer called (33) Q8. Give the difference between Constructor and Destructor. Ans. Difference Between Constructor and Destructor’ Constructor \ Destructor Does take the argument. Class can have mutliple constructor. Constructor can be overloaded. Constructor cannot be a virtual. Name is same as class name. Constructor function invoked automatically when the object created. Does not take argument. Class can have only one destructor. 3. Destructor can not be overloaded. 4. - Destructor can be virtual. 5. Name is same as class name prefixed by tilde (~) symbol. 6. Invoked automatically before terminating the program. (35) Q 11. What do you understand by Static Data Member ? Ans. : Static Data Member : A data member of a class can be qualified as static. The properties of a static member variable are similar to that of a C static variable. A static member variable has certain special characteristics. These are : It is initialized to zero when the first object of its class is created. No other initialization, is permitted. Only one copy of that member is-created for the entire class and is shared by all the objects of that class. It is visible only within the class, but its lifetime is the entire program. Static variables are normally used to maintain values common the entire class. For example, a static data member can be used as counter that records the occurences of all the objects. Program illustrates the use of a static data memmber : Static class member # include # include Class item : { Static int count; int number; Public : void getdata (int a) { number = a; - count ++; } void getcount (void) { count <<“count :”; count < # include class test { int code; static int count; // static member variable Public : void setcode (void) code = ++ count; void showcode (void) { & ~ count <<“object number : “< void get( int one, int two=2, int three=3); void get(int one, int two, int three) { cout <<“One =“< void main(void) { int *ptr; ptr=new int(10); cout<<*ptr; delete ptr; ae is will-allocate memory for an int(eger) having initial value 10, pointed by the ptr pointer. Memory space for arrays is allocated as shown below: ptr=new DataType [x]; Here, . e ptr and DataType have the same meaning as above. e xis the number of elements and C is a constant. Ex. /fExample Program in C++ #include void main(void) { int *ptr, size; ; (41) cin>>size; ptr=new int[size]: /arrays are freed-up like this delete []ptr: } ; Freeing-Up Allocated Memory: Unlike static variables, C++ will not free-up the memory allocated through dynamic allocation. Its your duty to free them up. delete keyword is used to free-up the allocated memory. delete ptr; Arrays are deleted in a slightly different manner as shown below: delete []ptr; It’s easy to forget to free the allocated memory because C++ compiler won’t inform you that you are doing this. It’s your job and should always be done. : Now let me show you an example of dynamic memory allocation in action: - //Example Program in C++ #include void main(void) { ee a EHOW many PH. Numbers do you wish to enter:”; cin>>size; . \ptr=new int[size];//allocate memory //imput ph. numbers for (int i=0;i>ptr[i]; output ph. numbers ie cout<<“\n\n\n PH. NOs. are\n”s i=0;i

You might also like