Answers for "class 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
5

c++ class method example

class Object {
public:
	int var;
	void setVar(int n) {
		var = n;
	}
	int getNum() {
		return var;
	}
};

int main() {
	Object obj;
	obj.setVar(13);
	std::cout << obj.getNum() << std::endl;
	return 0;
}
Posted by: Guest on January-18-2020
2

class cpp

class Exemple
{
  private:
  	/* data */
  public:
  	Exemple(); //Constructor
  	~Exemple(); //Destructor
};
Posted by: Guest on January-14-2021
0

c++ classes

class SampleClass
{
    int a;
public:
    SampleClass(int v)
    {
        a=v;
    }
    int RetVal()
    {
        return a;
    }
};
Posted by: Guest on April-12-2021
0

deifine an object in C++

classname identifier;
Posted by: Guest on August-19-2020
0

vector in c++ class

#include <iostream>
#include <vector>

class Object
{
	public:
		Object()
		{}
		~Object()
		{}
		void AddInt(int num)
		{
			m_VectorOfInts.push_back(num);
		}
		std::vector<int> GetCopyOfVector()
		{
			return m_VectorOfInts;
		}
		void DisplayVectorContents()
		{
			for( unsigned int i = 0; i < m_VectorOfInts.size(); i++ )
			{
				std::cout << "Element[" << i << "] = " << m_VectorOfInts[i] << std::endl;
			}
			std::cout << std::endl;
		}

	private:
		std::vector<int> m_VectorOfInts;
};

int main()
{
	// Create our class an add a few ints
	Object obj;
	obj.AddInt(32);
	obj.AddInt(56);
	obj.AddInt(21);

	// Display the vector contents so far
	obj.DisplayVectorContents();

	// Creates a copy of the classes container you can only really view whats in 
	// the classes vector container. What ever changes you make here wont effect the class.
	std::vector<int> container1 = obj.GetCopyOfVector();
	// These elements wont be added as it's a copy of the container
	container1.push_back(342);
	container1.push_back(64);
	container1.push_back(123);


	// Display the classes container to see show nothing was added.
	obj.DisplayVectorContents();

	return 0;
}
Posted by: Guest on February-27-2020

Browse Popular Code Answers by Language