Basics

  1. What is C++?

C++ is an abstraction programming language (not just an extension of C but a High Level abstraction language that comes at no cost in performance )

  1. Who calls main()

C or C++ runtime library calls main(), example in GCC C , main is called by _start() function

  1. What are the process in compilation

preprocessing , compiling , assembling , linking

->preprocessing does text replacement , # define , and #includes are replaced

-> compiling takes individual files and converts to intermediate file

-> assembling converts the text file to assembly (target machine dependent example x86 or arm)

-> creates a single binary output file

  1. data types and standard ?

-> built in data types / primary data types

-> user defined data type

bool, char (unsigned / signed char char8_t, char16_t char32_t wchar_t etc) , int float , double

Standard enforces a minimum size but implementation can be compiler dependent

don't assume if standard doesn't specify something it will be implementation dependent and will result in ambiguous results

  1. Initialization older versus Uniform initialization

unsigned int a= -20 ; // no error with old initialization

with unsigned int a{-20}; // error detected , narrowing not allowed implicitly

  1. type deduction using auto and decltype(fn()) x;

use auto for initializer while decltype for complex expressions and function return

example : auto d{12.2}; // this is double

auto f_value{12.2F}; //this is float

using auto a= {123}; // results in list of integer

becomes -> std::initiliser_list<int> a = std::initiliser_list<int> {111};

using : int a={123}; // directly will result in initializer not as list but normal int , only with auto it works

  1. using auto makes type deduction , makes code easier to read and makes it easier to change

code:

#include<iostream>
#include<vector>
#include<list>

void func(std::vector<int> &a)
{

for(std::vector<int>::iterator i = a.begin(); i!= a.end();  ++i)
std::cout<<*i;

}

int main(){
std::list<int> p ={1,2,3,4,5}; // list of int
std::vector<int> q = {1,2,3,4,5,6,7};

func(q);
//func(&q);

}
//////////////////////////////////////

//using auto keyword

#include<iostream>
#include<vector>
#include<list>

void func(std::list<int> &a)
{

for(auto i = a.begin(); i!= a.end();  ++i)
std::cout<<*i;

}

int main(){
std::list<int> p ={1,2,3,4,5}; // list of int
std::vector<int> q = {1,2,3,4,5,6,7};

//func(q);
func(&q);

}

using decltype is for type deduction if no initial value is known but type of an function or expression needs to be evaluated

note : to print type in C++ use : typeid(i).name(); // if i is int d is printed for float F is printed

  1. pvallue , lvalue , prvalue , gvalue , xvalue

lvalue has a name / identity and usually present to the left of an expression or assignment

rvalue (a value thats returned from a function for example) doesnt have an identity and is not an lvalue

prvalue: pure r value is a subset of rvalue , same like rvalue it doesnt have a name / memory address that can be identified example : int a = 1+2 ; // 1+2 is a prvalue

int {a+b}; // is prvalue , no identity but expression

glvalue : similar to lvalue , the expression computes to an object or a function

xvalue : expiring vaue (like out of scope object?) value that has an identity but can be reused

  1. nullptr is of type std::null_ptr and can be used for ptr only unline NULL that can represent 0 (int) as well as NULL for pointer

Last updated