0% found this document useful (0 votes)
35 views6 pages

C++ Output Prediction

The document contains multiple C++ code snippets demonstrating various programming concepts such as boolean operations, string handling, namespaces, function overloading, and default arguments. Each snippet illustrates specific behaviors and outcomes when executing the code, highlighting the nuances of C++ syntax and functionality. The examples range from simple boolean manipulations to more complex function definitions and namespace usage.

Uploaded by

DARSHAN BALAPURE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views6 pages

C++ Output Prediction

The document contains multiple C++ code snippets demonstrating various programming concepts such as boolean operations, string handling, namespaces, function overloading, and default arguments. Each snippet illustrates specific behaviors and outcomes when executing the code, highlighting the nuances of C++ syntax and functionality. The examples range from simple boolean manipulations to more complex function definitions and namespace usage.

Uploaded by

DARSHAN BALAPURE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Bool

--------

1)#include<iostream>
using namespace std;
int main()
{
bool i=true;
cout<<i-1<<endl;
}

2)#include<iostream>
using namespace std;
int main()
{
bool i=10;
if(i>sizeof(i))
cout<<"vector"<<endl;
else
cout<<"india"<<endl;
}

3)#include<iostream>
using namespace std;
int main()
{
bool i=15;
i=i<<1;
i=i>>1;
cout<<i<<endl;
}

4)#include<iostream>
using namespace std;
int main()
{
bool i=0;
i=i<<1;
cout<<i<<endl;
}

5)#include<iostream>
using namespace std;
int main()
{
bool i='0';
cout<<(int)i<<endl;
}
Reference
---------------

1)#include <iostream>
using namespace std;
int main() {
int i=10;
const int &j=i;
cout<<j++<<" "<<i<<endl;
}

2)#include <iostream>
using namespace std;
int main()
{
int i=5;
int &p=i;
int *ptr = &p;
*ptr=p<<3;
cout<<i<<endl;
}

3)#include <iostream>
using namespace std;
int main()
{
const int &p=0;
cout<<p<<endl;
}

4)#include <iostream>
using namespace std;
int main()
{
int i=2;
const int *p=&i;
const int * &q=p;
i++;
cout<<*q<<endl;
}

5)#include <iostream>
using namespace std;
int main()
{
int a[5]={0};
int (&a1)[3]=a;
cout<<a1[0]<<endl;
}
6)#include <iostream>
using namespace std;
int main()
{
int a[5]={1,0,2};
int (&a1)[5]=a;
cout<<-a1[2]++<<endl;
cout<<-++0[a]<<endl;
}
string

1)#include <iostream>
using namespace std;
int main() // input is vector
{
string s;
cin.getline(s,3);
cout<<s<<endl;
}

2)#include <iostream>
using namespace std;
int main() // input is vector
{
char s[5];
cin.getline(s,3);
cout<<s<<endl;
}

3)#include <iostream>
using namespace std;
int main() // input is vector
{
string s;
getline(cin, s);
s = s.substr(0, 3);
cout << s << endl;
}

4)#include <iostream>
using namespace std;
int main() // input is vector
{
string s;
cout<<s.length()<<" "<<s.size()<<endl;}
5)#include <iostream>
using namespace std;
int main() // input is vector
{
char s[5];
cout<<s.length()<<" "<<s.size()<<endl
6)#include <iostream>
#include<cstring>
using namespace std;
int main()
{
char s[7];
cout<<strlen(s)<<" "<<sizeof(s)<<endl;
}

namespace

1)#include <iostream>
#include<cstring>
using namespace std;
namespace A
{int i=1,j=2;}
namespace B
{int i=3,j=4;}
int main()
{
cout <<i<<endl;
}

2)#include <iostream>
#include<cstring>
using namespace std;
namespace A
{int i=1,j=2;}
namespace B
{int i=3,j=4;}
using namespace A;
int main()
{
cout <<i<<endl;
}

3)#include <iostream>
#include<cstring>
using namespace std;
namespace A
{int i=1,j=2;}
namespace B
{int i=3,j=4;}
using namespace A;
int main()
{
cout <<B::i<<endl;
}
4)#include <iostream>
#include<cstring>
using namespace std;
namespace A
{int i=1,j=2;}
namespace B
{int i=3,j=4;}
using namespace A;
int main()
{
using namespace B;
cout <<i<<endl;
}

5)#include <iostream>
#include<cstring>
using namespace std;
namespace A
{int i=1,j=2;}
namespace B
{int i=3,j=4;}

int main()
{
using namespace B;
cout <<i<<endl;
using namespace A;
}

function overloading

1)#include <iostream>
using namespace std;
void fun(float x)
{ cout <<x<<endl;}
void fun(double x)
{ cout <<x<<endl;}
int main() {
fun(5);}

2)#include <iostream>
using namespace std;
void fun(float x,int y)
{ cout <<x<<endl;}
void fun(double x,int y)
{ cout <<x<<endl;}
int main() {
fun(5.2,3);}
3)#include <iostream>
using namespace std;
void fun(int y)
{ cout <<y<<endl;}
void fun(int &y)
{ cout <<y<<endl;}
int main() {
fun(3);
}

default arguments

1)#include <iostream>
using namespace std;
void fun(int y=10,int z)
{ cout <<y<<endl;}
int main() {
int i=3;
fun(i);
}

2)#include <iostream>
using namespace std;
void fun(int y,int z=2)
{ return y+z;}
int main() {
int i=3;
cout<<fun(i)<<endl;
}

3)#include <iostream>
using namespace std;
int fun(int y,int z)
{ return y+z;}
int fun(int y)
{ return y;}
int main() {
int i=3;
cout<<fun(i)<<endl;
cout<<fun(i,3)<<endl;
}

You might also like