OOP T03 715

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

can you access private members without using friend. ye

#include<iostream> using namespace std; class Test { private: int data; public: Test() { data = 0; } int getData() { return data; } }; int main() { Test t; int* ptr = (int*)&t; *ptr = 10; cout << t.getData(); return 0; }

static int count

#include<iostream> using namespace std; class Test { private: int data; public: Test() { data = 0; } int getData() { return data; } }; int main() { Test t; int* ptr = (int*)&t; *ptr = 10; cout << t.getData(); return 0; }

copy constructor

A copy constructor is a member function which initializes an object using another object of the same class. A copy constructor has the following general function prototype: rec(const rec & copy) x = copy.x y = copy.y copy constructor can be private.

Encapsulation

Encapsulation is defined as wrapping up of data and information under a single unit. In Object Oriented Programming, Encapsulation is defined as binding together the data and the functions that manipulates them. using namespace std; class Encapsulation { private: // data hidden from outside world int x; public: // function to set value of // variable x void set(int a) { x =a; } // function to return value of // variable x int get() { return x; } }; // main function int main() { Encapsulation obj; obj.set(5); cout<<obj.get(); return 0; }

Access modifier

Public: All the class members declared under public will be available to everyone. Private: The class members declared as private can be accessed only by the functions inside the class. Protected: Protected access modifier is similar to that of private access modifiers, the difference is that the class member declared as Protected are inaccessible outside the class but they can be accessed by any subclass(derived class) of that class. friend can access public inherited class can access protected

def

constructor - a member function that initializes object of a class friend - can access private and protected members of other class inheritance - class to derive properties and characteristics from another class overloading - multiple definitions of the function by changing parameters override - redefinition of base class function in its derived class polymorphism - overriding a operator is an example of protected - data hiding static - preserves the value even after they are out of scope this - a constant pointer that holds the memory address virtual - a member function which is declared within the base class and re defined.

operator overloading

friend ostream& operator(stream& os, rec r){ os << return os; rec operator +(const rec& rhs) x = x.rhs y = y.rhs 1) For operator overloading to work, at leas one of the operands must be a user defined class object. 2) Assignment Operator: Compiler automatically creates a default assignment operator with every class. The default assignment operator does assign all members of right side to the left side and works fine most of the cases (this behavior is same as copy constructor). See this for more details. 3) Conversion Operator: We can also write conversion operators that can be used to convert one type to another type.

PolyMorphisim

polymorphism means having many forms. In simple words, we can define polymorphism as the ability of a message to be displayed in more than one form. Compile time Polymorphism Function Overloading Operator Overloading Runtime Polymorphism Function overriding on the other hand occurs when a derived class has a definition for one of the member functions of the base class. That base function is said to be overridden. using namespace std; class base { public: virtual void print () { cout<< "print base class" <<endl; } void show () { cout<< "show base class" <<endl; } }; class derived:public base { public: void print () //print () is already virtual function in derived class, we could also declared as virtual void print () explicitly { cout<< "print derived class" <<endl; } void show () { cout<< "show derived class" <<endl; } }; //main function int main() { base *bptr; derived d; bptr = &d; //virtual function, binded at runtime (Runtime polymorphism) bptr->print(); // Non-virtual function, binded at compile time bptr->show(); return 0; }

https://www.geeksforgeeks.org/basic-concepts-of-object-oriented-programming-using-c/ https://www.geeksforgeeks.org/encapsulation-in-c/ https://www.geeksforgeeks.org/polymorphism-in-c/ https://www.geeksforgeeks.org/inheritance-in-c/ https://www.geeksforgeeks.org/abstraction-in-c/ https://www.geeksforgeeks.org/operator-overloading-c/ https://www.geeksforgeeks.org/access-modifiers-in-c/ https://www.geeksforgeeks.org/virtual-function-cpp/ https://www.geeksforgeeks.org/early-binding-late-binding-c/ https://www.geeksforgeeks.org/copy-constructor-in-cpp/ https://www.geeksforgeeks.org/friend-class-function-cpp/ https://www.geeksforgeeks.org/can-access-private-data-members-class-without-using-member-friend-function/ https://www.geeksforgeeks.org/count-number-objects-using-static-member-function/

1

virtual function

A virtual function a member function which is declared within base class and is re-defined (Overriden) by derived class. Virtual functions ensure that the correct function is called for an object, regardless of the type of reference (or pointer) used for function call. They are mainly used to achieve Runtime polymorphism Functions are declared with a virtual keyword in base class. The resolving of function call is done at Run-time. using namespace std; class base { public: virtual void print () { cout<< "print base class" <<endl; } void show () { cout<< "show base class" <<endl; } }; class derived:public base { public: void print () { cout<< "print derived class" <<endl; } void show () { cout<< "show derived class" <<endl; } };

abstraction

Abstraction means displaying only essential information and hiding the details. Data abstraction refers to providing only essential information about the data to the outside world, hiding the background details or implementation. We can implement Abstraction in C++ using classes. Class helps us to group data members and member functions using available access specifiers. A Class can decide which data member will be visible to outside world and which is not. Abstraction using Classes: We can implement Abstraction in C++ using classes. Class helps us to group data members and member functions using available access specifiers. A Class can decide which data member will be visible to outside world and which is not. Abstraction in Header files: One more type of abstraction in C++ can be header files. For example, consider the pow() method present in math.h header file. Whenever we need to calculate power of a number, we simply call the function pow() present in the math.h header file and pass the numbers as arguments without knowing the underlying algorithm according to which the function is actually calculating power of numbers. class implementAbstraction { private: int a, b; public: // method to set values of // private members void set(int x, int y) { a = x; b = y; } void display() { cout<<"a = " <<a << endl; cout<<"b = " << b << endl; } }; int main() { implementAbstraction obj; obj.set(10, 20); obj.display(); return 0; } Advantages of Data Abstraction: Helps the user to avoid writing the low level code Avoids code duplication and increases reusability. Can change internal implementation of class independently without affecting the user. Helps to increase security of an application or program as only important details are provided to the user.

early binding late binding

Early Binding (compile-time time polymorphism) As the name indicates, compiler (or linker) directly associate an address to the function call. It replaces the call with a machine language instruction that tells the mainframe to leap to the address of the function. Any normal function call (without virtual) // is binded early. Here we have taken base // and derived class example so that readers // can easily compare and see difference in // outputs. Late Binding : (Run time polymorphism) In this, the compiler adds code that identifies the kind of object at runtime then matches the call with the right function definition (Refer this for details). This can be achieved by declaring a virtual function. using namespace std; class Base { public: virtual void show() { cout<<" In Base \n"; } }; class Derived: public Base { public: void show() { cout<<"In Derived \n"; } }; int main(void) { Base *bp = new Derived; bp->show(); // RUN-TIME POLYMORPHISM return 0; }

friend class

Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful to allow a particular class to access private members of other class. For example a LinkedList class may be allowed to access private members of Node. cannot be inherited cannot be mutual Friend Function Like friend class, a friend function can be given special grant to access private and protected members. A friend function can be: a) A method of another class b) A global function filter_none edit play_arrow brightness_4 class Node { private: int key; Node *next; /* Other members of Node Class */ friend int LinkedList::search(); // Only search() of linkedList // can access internal members };

Basic Concepts of OOP

Object: Objects are basic run-time entities in an object oriented system, objects are instances of a class these are defined user defined data types. ex: class person { char name[20]; int id; public: void getdetails(){} }; int main() { person p1; //p1 is a object } Each object contains data and code to manipulate the data. Objects can interact without having to know details of each others data or code Encapsulation and Data abstraction: Wrapping up(combing) of data and functions into a single unit is known as encapsulation. The data is not accessible to the outside world and only those functions which are wrapping in the class can access it. This insulation of the data from direct access by the program is called data hiding or information hiding. Data abstraction refers to, providing only needed information to the outside world and hiding implementation details. For example, consider a class Complex with public functions as getReal() and getImag(). We may implement the class as an array of size 2 or as two variables. The advantage of abstractions is, we can change implementation at any point, users of Complex class wont't be affected as out method interface remains same. Had our implementation be public, we would not have been able to change it. Inheritance: inheritance is the process by which objects of one class acquire the properties of objects of another class. It supports the concept of hierarchical classification. Inheritance provides re usability. This means that we can add additional features to an existing class without modifying it. Polymorphism: polymorphism means ability to take more than one form. An operation may exhibit different behaviors in different instances. The behavior depends upon the types of data used in the operation. C++ supports operator overloading and function overloading. Operator overloading is the process of making an operator to exhibit different behaviors in different instances is known as operator overloading. Function overloading is using a single function name to perform different types of tasks. Polymorphism is extensively used in implementing inheritance. Dynamic Binding: In dynamic binding, the code to be executed in response to function call is decided at runtime. C++ has virtual functions to support this. Message Passing: Objects communicate with one another by sending and receiving information to each other. A message for an object is a request for execution of a procedure and therefore will invoke a function in the receiving object that generates the desired results. Message passing involves specifying the name of the object, the name of the function and the information to be sent.

inheritance

The capability of a class to derive properties and characteristics from another class is called Inheritance. Inheritance is one of the most important feature of Object Oriented Programming. Sub Class: The class that inherits properties from another class is called Sub class or Derived Class. Super Class:The class whose properties are inherited by sub class is called Base Class or Super class. Single Inheritance: In single inheritance, a class is allowed to inherit from only one class. i.e. one sub class is inherited by one base class only. // C++ program to demonstrate implementation // of Inheritance //Base class class Parent { public: int id_p; }; // Sub class inheriting from Base Class(Parent) class Child : public Parent { public: int id_c; }; //main function int main() { Child obj1; // An object of class child has all data members // and member functions of class parent obj1.id_c = 7; obj1.id_p = 91; cout << "Child id is " << obj1.id_c << endl; cout << "Parent id is " << obj1.id_p << endl; return 0; } Multilevel Inheritance: In this type of inheritance, a derived class is created from another derived class. class Vehicle { public: Vehicle() { cout << "This is a Vehicle" << endl; } }; class fourWheeler: public Vehicle { public: fourWheeler() { cout<<"Objects with 4 wheels are vehicles"<<endl; } }; // sub class derived from two base classes class Car: public fourWheeler{ public: car() { cout<<"Car has 4 Wheels"<<endl; } }; // main function int main() { //creating object of sub class will //invoke the constructor of base classes Car obj; return 0; }


Kaugnay na mga set ng pag-aaral

Compound Subjects, Predicates, and Sentences.

View Set

Customer Accounts--Documentation

View Set

Which of the following formula is represents a molecular compound

View Set

Chapter 2 - Business Ethics and Social Responsibility

View Set

Cross Cultural Organizational Psychology

View Set