0% found this document useful (0 votes)
27 views

Call by Value:: When The Above Code Is Put Together in A File, Compiled and Executed, It Produces The Following Result

The document discusses call by value and call by reference in C++. Call by value passes arguments to a function as copies, so any changes made to the parameters inside the function are not reflected outside. Call by reference passes the address of arguments, allowing changes made to the parameters inside the function to be visible outside. The examples show that when calling a function to swap two integers using call by value, the original values outside remain unchanged, but when using call by reference, the original values are swapped.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Call by Value:: When The Above Code Is Put Together in A File, Compiled and Executed, It Produces The Following Result

The document discusses call by value and call by reference in C++. Call by value passes arguments to a function as copies, so any changes made to the parameters inside the function are not reflected outside. Call by reference passes the address of arguments, allowing changes made to the parameters inside the function to be visible outside. The examples show that when calling a function to swap two integers using call by value, the original values outside remain unchanged, but when using call by reference, the original values are swapped.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

CALL BY VALUE:

#include <iostream>
using namespace std;
// function declaration
void swap(int x, int y);
int main ()
{
// local variable declaration:
int a = 100;
int b = 200;
cout << "Before swap, value of a :" << a << endl;
cout << "Before swap, value of b :" << b << endl;
// calling a function to swap the values.
swap(a, b);
cout << "After swap, value of a :" << a << endl;
cout << "After swap, value of b :" << b << endl;
}

return 0;

When the above code is put together in a file, compiled and executed, it produces the
following result:
Before swap, value of a :100
Before swap, value of b :200
After swap, value of a :100
After swap, value of b :200

CALL BY REFERENCE :
#include <iostream>
using namespace std;
// function declaration
void swap(int &x, int &y);
int main ()
{
// local variable declaration:
int a = 100;
int b = 200;
cout << "Before swap, value of a :" << a << endl;
cout << "Before swap, value of b :" << b << endl;
/* calling a function to swap the values using variable reference.*/
swap(a, b);
cout << "After swap, value of a :" << a << endl;
cout << "After swap, value of b :" << b << endl;
return 0;

When the above code is put together in a file, compiled and executed, it produces the
following result:
Before swap, value of a :100
Before swap, value of b :200
After swap, value of a :200
After swap, value of b :100

#include <iostream.h>
#include<conio.h>
class Car
{
private:
Car(){};
int n1;
public:
Car(int n2)
{
n1=n2;
}
void printNo()
{
cout<<n1<<endl;
}
};
void printCarNumbers(Car *cars, int length)
{
for(int i = 0; i<length;i++)
cout<<cars[i].printNo();
}
int main()
{
int userInput = 10;
Car *mycars = new Car[userInput];
for(int i =0;i < userInput;i++)
mycars[i]=new Car[i+1];
printCarNumbers(mycars,userInput);
return 0;
}

I want to create a car array but I get the following error:


cartest.cpp: In function int main():
cartest.cpp:5: error: Car::Car() is private
cartest.cpp:21: error: within this context

You might also like