top of page

C++ interview basics

  • Writer: sondip poul singh
    sondip poul singh
  • May 15, 2019
  • 6 min read

#include <iostream> using namespace std;

int main() { //some bitwise operation int a=5,b=6; cout<<(a||b)<<" "<<(a|b)<<" "<<~(a|b)<<endl; //1,7,-8 a|b means bitwise or ~means bitwise not a=00000101 b=00000110 ----------- r=00000111(7) ~=11111000(248) which is -8 (so the rule is simple the negative value is equal to -(num+1) as 7 turns into -8,1 will turn into -2,100 will turn into -101 this happend cause int is 4 byte=32 bit so ~7 becomes 111...11111000(32 bit) which is -8 but if it is 8 bit only like 11111000 it is 248 return 0; }

Signed vs Unsigned:

Signed variables, such as signed integers will allow you to represent numbers both in the positive and negative ranges.

Unsigned variables, such as unsigned integers, will only allow you to represent numbers in the positive.

For example, an unsigned byte can represent values from 0 to 255, while signed byte can represent -128 to 127.

While commonly referred to as a 'sign bit', the binary values we usually use do not have a true sign bit.

Most computers use two's-complement arithmetic. Negative numbers are created by taking the one's-complement (flip all the bits) and adding one:

5 (decimal) -> 00000101 (binary) 1's complement: 11111010 add 1: 11111011 which is 'FB' in hex

This is why a signed byte holds values from -128 to +127 instead of -127 to +127:

1 0 0 0 0 0 0 0 = -128 1 0 0 0 0 0 0 1 = -127 - - - 1 1 1 1 1 1 1 0 = -2 1 1 1 1 1 1 1 1 = -1 0 0 0 0 0 0 0 0 = 0 0 0 0 0 0 0 0 1 = 1 0 0 0 0 0 0 1 0 = 2 - - - 0 1 1 1 1 1 1 0 = 126 0 1 1 1 1 1 1 1 = 127 (add 1 to 127 gives:) 1 0 0 0 0 0 0 0 which we see at the top of this chart is -128.

->for int(4 bytes=32 bit) we get 2^32-1= 0 to 4294967295(unsigned) and -(2^31) to (2^31)-1 ->for short int(2 bytes) unsigned=0 to 2^16-1 //65535 signed=(2^15) to (2^15)-1 //-32768 to 32767 ->for 1 byte unsigned(0 to 2^8-1) //255 signed(2^7) to (2^7)-1 //-128 to 127

In c++ declaring->unsigned int a=10; cout<<unsigned(a) will print the unsigned value

*********************************************************************

#C++ pointer vs Reference:

reference hocche kono existing variable er alternative name. declaration is int x=10; int &a=x;(reference) akhon jodi a=100 likhi taile x=100 e hobe

reference vs pointer: reference; 1.safe karon surute initialize kortei hobe jmn int &x=a liksi 2.easier to use;dereferecing lage na pointer er moto

osubidha in reference: 1.initialize kora lage 2.null hoite pare na 3.akbar create hoile pore oi reference r kono object k refer korte parena

#A virtual function a member function which is declared within a base class and is re-defined(Overriden) by a derived class.

declare a function in the base class override it on the derived class make a base class pointer in the main function call the derived class function using the base class pointer if we not declared it as virtual then the pointer will the base class function though we give it reference of the derived class object.To access the function of derived class use virtual keyword in the base class.

virtual class run time a kaj kore ai jonno dynamic memory allocation kore. no virtual class gula compile time a allocate kore tai take bole static binding.Static binding a amra memory size age thekei jani.

#include<bits/stdc++.h> using namespace std;

class person{ public: virtual void pr(){ cout<<"i am base class"<<endl; } };

class student: public person { public: void pr(){ cout<<"i am student derived class"<<endl; }

};

int main() { //simple function call by creating objects person q; q.pr(); student susmoy; susmoy.pr(); //using pointer person *p; student sondip; p=&sondip; p->pr(); //i am student derived class return 0; }

#class and Object: Class is a user defined data type, which holds its own data members and member functions, which can be accessed and used by creating an instance of that class. A class is like a blueprint for an object.An Object is an instance of a Class. When a class is defined, no memory is allocated but when it is instantiated (i.e. an object is created) memory is allocated.

#constructor,destructor: https://www.geeksforgeeks.org/c-classes-and-objects/

#reson of using copy constructor:

copy constructor akta new object make kore with the exact copy of the same class object. The copy constructor is used to: Initialize one object from another of the same type. Copy an object to pass it as an argument to a function. and also for returniing a object

#Run time error vs complile time error: (A compiler is a special program that processes statements written in a particular programming language and turns them into machine language or "code" that a computer's processor uses.) so source code theke jokhon machine code a convert korte jai tokhn jodi dekhe j error ache tokhon take bole compile time error.Most common error hocche semicolon na deya,bracket thikmoto end na kora,type mismatch(int a declare kore float value rakha).

After a successful compilation when the compiled code start executing,if there is any errors then we call it as run time error.compile time a ai error gula dhora porena,jmn division by zero,finding square root of a negative number,dereferencing a null pointer.

#compiler vs interpreter: compiler or interpreter both are used to make hign level language to low level machine code that will understood by the computer or any virtual program for execution.as example c++ souce code will be assembly code,java code will be byte code etc.

let us jump into compiler:compiler take the whole source code and make it a low level code.

source->compiler(black box)->machine level code

So whats in the black box? ->lexical analysis(tokenaization) ->syntax(abstract syntax tree from token) ->symantics(souce code rule checking,annotated syntax tree) ->intermediate code generation(three address code) ->optimazation(optimize) ->code generation(assembly code)

first 4 are front end and last 2 are back end.just saying.

interpreter takes the souce code statements one by one and immedietly execute it.

difference between them are: 1.com takes the whole source code whereas int. takes statement by statement 2.com. takes lot of time to make the assembly code but its overall execution time is faster than interpreter. 3.com takes extra memory cause it store the intermediate code and linking hence int. are memory efficient 4.com gives error(if any) after checking the whole source code,int. gives it immedietly 5.c,c++ use compiler,Python,Ruby use interpreter.

#assembler,linker,loader: compiler er assembly code k machine code a niye jai assembler,a special kind of compiler, mainly assembler .o(object file) create kore. Linker akadhik .o file(jegula lagbe) combine kore executable file a pathai.loader executable file gula k main memory te pathai execution er jonno.(.exe file).

#dynamic memory and static memory: To conclude above topic, static memory is something that compiler allocates in advance. While dynamic memory is something that is controlled by the program during execution or run time.

OOP are base 3 ta: 1.encapsulation(data hiding+method r varibale aksathe) 2.inheritence(base class->derived class) multiple(akta class onek class a inherit korle) DIAMOND PROBLEM aikhan thekei hoi Example: class Mammal { public: Mammal() { cout << "Mammals can give direct birth." << endl; } };

class WingedAnimal { public: WingedAnimal() { cout << "Winged animal can flap." << endl; } };

class Bat: public Mammal, public WingedAnimal {

};

multilevel(a->b b->c k inherit korlo amn type): class A { ... .. ... }; class B: public A { ... .. ... }; class C: public B { ... ... ... };

Polymorphism: aki jiniser vinno vinno rup.jmn add function diye alada alada parameter bosai kaj kora jai.Function overloading hocche compile time polymorphism er akta rup. Function overloading manei kintu aki namer function er vinno vinno kaj parameter er upor vitti kore.Function Overriding hocche run time polymorphism. Function overriding er kitabi songa hocche "Function overriding is a feature that allows us to have a same function in child class which is already present in the parent class.".Pointed diye call dile derived class er override function ta kaj korena,base class er tai kaj korte thake,Ai problem dur korte virtual vabe declare korte hoi base class er function k.

inline function: akta function k inline likle jkhn kono jaigai oi function k call kora hoi compiler pura code tai oi jaigai copy kore rakhe. inline hisabe declare na korle function ta k serach korte kisu somoi jai,aijonno time bachate kisu khetre inline function call kora jai,function choto hoile call kora valo,naile barabr jodi oi function k call kora hoi tahole onek memory loss hoi,inline declare korleo compiler seta k inline ba copy kore rakbe kina ta thik nai,compiler er icchar upor depend kore.

friend function: kono akta function ja kono nirdisto class er nijer function na howa sotteo tar private r protected member keo access korte pare.A jodi B er friend function hoi taile sudhu A e kintu access korte parbe,B parbe na.

Abstraction:hiding the implementation details to user and only shows whats necessary.like button a click korle amnitei like bare jai fb te,pichone kivabe kaj kortese janar dorkar nai user er.

Abstract class:kono class a jodi akta pure virtual function theke take abstract class bole.pure virtual bolte bujhase j amn akta function thakbe jat kono body thakbe na. virtual int sum()=0 //0 mane compiler bujhe body nai abstract class k call kora.

 
 
 

コメント


bottom of page