Answers for "new object c++"

C++
2

declaring instance of class c++

#include <iostream>     
using namespace std;

class Foo
 {
   public:
   Foo ( )
   {
      cout << "constructor Foon";
   }               
};

class Bar
 {
   public:
   Bar ( Foo )
   {
      cout << "constructor Barn";
   }
};

int main()
{
   /* 1 */ Foo* foo1 = new Foo ();
   /* 2 */ Foo* foo2 = new Foo;
   /* 3 */ Foo foo3;
   /* 4 */ Foo foo4 = Foo::Foo();

   /* 5 */ Bar* bar1 = new Bar ( *new Foo() );
   /* 6 */ Bar* bar2 = new Bar ( *new Foo );
   /* 7 */ Bar* bar3 = new Bar ( Foo foo5 );
   /* 8 */ Bar* bar3 = new Bar ( Foo::Foo() );

   return 1;
}
Posted by: Guest on January-07-2021
0

what is an object in cpp

#include<iostream>
using namespace std;
class Yolo
{
  int m;
  public:
  void input()
  {
    cout<<"enter any number : ";
    cin>>m;
  }
  void display()
  {
    cout<<"Entered Number : "<<m<<endl;
  }
};

int main()
{
  Yolo obj;
  obj.input();
  obj.display();
}
Posted by: Guest on November-28-2021
0

new class *

/*
 Keyword "this"
You can use keyword "this" to refer to this instance inside a class definition.

One of the main usage of keyword this is to resolve ambiguity between the names of 
data member and function parameter. For example:
*/
class Circle {
private:
   double radius;                 // Member variable called "radius"
   ......
public:
   void setRadius(double radius) { // Function's argument also called "radius"
      this->radius = radius;
         // "this.radius" refers to this instance's member variable
         // "radius" resolved to the function's argument.
   }
   ......
}
Posted by: Guest on January-01-1970

Browse Popular Code Answers by Language