• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to secondary sidebar

GoHired

Interview Questions asked in Google, Microsoft, Amazon

Join WeekEnd Online Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

  • Home
  • Best Java Books
  • Algorithm
  • Internship
  • Certificates
  • About Us
  • Contact Us
  • Privacy Policy
  • Array
  • Stack
  • Queue
  • LinkedList
  • DP
  • Strings
  • Tree
  • Mathametical
  • Puzzles
  • Graph

C++ OOPs Part1

November 5, 2014 by Dhaval Dave

ABSTRACTION , INHERITANCE , ENCAPSULATION

ABSTRACTION :  

Data abstraction refers to, providing only essential information to the outside world and hiding their background details
Any C++ program where you implement a class with public and private members is an example of data abstraction
-Create your own example.

INHERITANCE : 

A class can be derived from more than one classes, which means it can inherit data and functions from multiple base classes. To define a derived class, we use a class derivation list to specify the base class(es). A class derivation list names one or more base classes and has the form:

class derived-class: access-specifier base-class

Where access-specifier is one of public, protected, or private, and base-class is the name of a previously defined class. If the access-specifier is not used, then it is private by default

#include <iostream>

using namespace std;
//Base Class
class Shape {
public:
void setWidth(int w)
{
width = w;
}
void setHeight(int h)
{
height = h;
}
protected:
int width;
int height;
};

// Derived Class
class Rectangle: public Shape
{
public:
int getArea()
{
return (width * height);
}
};

int main(void)
{
Rectangle Rect;

Rect.setWidth(5);
Rect.setHeight(7);

// Print the area of the object.
cout << "Total area: " << Rect.getArea() << endl;
return 0;
}

A C++ class can inherit members from more than one class and here is the extended syntax:

class derived-class: access baseA, access baseB….

class Rectangle: public Shape, public PaintCost

ENCAPSULATION

Data encapsulation is a mechanism of bundling the data, and the functions that use them and data abstraction is a mechanism of exposing only the interfaces and hiding the implementation details from the user.

C++ supports the properties of encapsulation and data hiding through the creation of user-defined types, called classes. We already have studied that a class can contain private, protected and public members. By default, all items defined in a class are private. For example:

class Box
{
public:
double getVolume(void)
{
return length * breadth * height;
}
private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};

The variables length, breadth, and height are private. This means that they can be accessed only by other members of the Box class, and not by any other part of your program. This is one way encapsulation is achieved.

Similar Articles

Filed Under: problem Tagged With: c

Reader Interactions

Primary Sidebar

Join WeekEnd Online/Offline Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

Join WeekEnd Online/Offline Batch from 4-April-2020

WhatsApp us

Secondary Sidebar

Custom Search

  • How I cracked AMAZON
  • LeetCode
  • Adobe
  • Amazon
  • Facebook
  • Microsoft
  • Hacker Earth
  • CSE Interview

Top Rated Questions

Get K Max and Delete K Max in stream of incoming integers

Maximum of all subarrays of size k

Add Sub Multiply very large number stored as string

Python String and numbers

SAP Off Campus Hiring_ March 2015 Sample Questions

LeetCode : Word Search

N teams are participating. each team plays twice with all other teams. Some of them will go to the semi final. Find Minimum and Maximum number of matches that a team has to win to qualify for finals ?

Generate next palindrome number

How Radix sort works

Walmart Labs Interview Experience

Find the smallest window in a string containing all characters of another string

Mirror of Tree

Get Minimum element in O(1) from input numbers or Stack

Word Break Problem

robot standing at first cell of an M*N matrix. It can move only in two directions, right and down. In how many ways, it can reach to the last cell i.e. (M, N) Code it

ADOBE Aptitude C Language Test

K’th Largest Element in BST when modification to BST is not allowed

Printing intermediate Integers between one element & next element of array

write a c program that given a set a of n numbers and another number x determines whether or not there exist two elements in s whose sum is exactly x

Stickler thief

Test Cases for Round Function

Given array of 0’s and 1’s. All 0’s are coming first followed by 1’s. find the position of first 1

Interfaces in C++ (Abstract Classes in C++)

Reliance Jio Software Developer Interview Experience

Possible sizes of bus to carry n groups of friends

Python Array String

Find the kth number with prime factors 3, 5 and 7

Practo Hiring Experience

Find the number ABCD such that when multipled by 4 gives DCBA.

Fibonacci Hashing & Fastest Hashtable

Copyright © 2026 · Genesis Framework · WordPress · Log in