Answers for "new function in c++"

C++
21

how to declare a function in c++

// function example
#include <iostream>
using namespace std;

int addition (int a, int b)
{
  int r;
  r=a+b;
  return r;
}
Posted by: Guest on March-04-2020
2

new c++

MyClass * p1 = new MyClass;
      // allocates memory by calling: operator new (sizeof(MyClass))
      // and then constructs an object at the newly allocated space

  MyClass * p2 = new (std::nothrow) MyClass;
      // allocates memory by calling: operator new (sizeof(MyClass),std::nothrow)
      // and then constructs an object at the newly allocated space

  new (p2) MyClass;
      // does not allocate memory -- calls: operator new (sizeof(MyClass),p2)
      // but constructs an object at p2

  // Notice though that calling this function directly does not construct an 
  //object:
  MyClass * p3 = (MyClass*) ::operator new (sizeof(MyClass));
      // allocates memory by calling: operator new (sizeof(MyClass))
      // but does not call MyClass's constructor

  delete p1;
  delete p2;
  delete p3;
Posted by: Guest on June-07-2020
2

How to make a function in C++

//first lets create a function
/*void is for starting something, anything after void will be the name of your
function which will be followed by () */
void yourFunction() {
//your code will be here, anything here will be the code in the yourFunction
  cout << "Functions"
}
//now we have to go to our main function, the only function the compiler reads
int main() {
  myFunction(); //you call the function, the code we put in it earlier will be executed
  return 0;
}
Posted by: Guest on November-01-2020
0

what is c++ function

return_type function_name( parameter list ) {
   body of the function
}
Posted by: Guest on July-16-2021
0

new in c++

#include <iostream>
#include <string>

using String = std::string;
class Entity
{
private:
	String m_Name;
public:
	Entity() : m_Name("Unknown") {}
	Entity(const String& name) : m_Name(name) {}
	const String& GetName() const {
		return m_Name;
	};
};
int main() {
  // new keyword is used to allocate memory on heap
	int* b = new int; // new keyword will call the c function malloc which will allocate on heap  memory = data and return a ptr to that plaock of memory
	int* c = new int[50];
	Entity* e1 = new Entity;//new keyword Not allocating only memory but also calling the constructor
	Entity* e = new Entity[50];
	//usually calling new will  call underlined c function malloc
	//malloc(50); 
	Entity* alloc = (Entity*)malloc(sizeof(Entity));//will not call constructor only  allocate memory = memory of entity
	delete e;//calls a c function free
	Entity* e3 = new(c) Entity();//Placement New
Posted by: Guest on June-16-2020
1

function in c++

#include <iostream>
using namespace std;

// Create a function
void myFunction() {
  cout << "I just got executed!";
}

int main() {
  myFunction(); // call the function
  return 0;
}

// Outputs "I just got executed!"
Posted by: Guest on May-24-2021

Browse Popular Code Answers by Language