printf() vs cout

Function vs Object

Table of contents

No heading

No headings in the article.

Hii Developers, From the beginning I was very confuse about printf() vs cout

printf() - it is a function, we can pass data and this function will print it on console

cout - It is object not a function, but if it is object then why we do not call it using dot operator and what's the name of function/method is called using cout object

Operator overloading - without the knowledge of operator overloading it is impossible to clearly understand what is cout do.

In C++,

1.cout<<"Hello C++" ;

is same as

2.cout.operator<<("Hello C++");

In statement 2nd cout object call overloaded function/method using << operator, call it insertion operator.

cout.write("hello cpp",10);

#include<iostream>

int main() {

std::cout.write("Hello world!",12);

return 0;

}

Or many more methods using with cout object

It is not just a replacement of printf .

It have many more capabilities with inbuilt awesome functions/methods