Question

Locked

Learning Object Oriented C

By The_Parasite ·
I'm trying to teach myself Object Oriented C++ (I know Funktion Oriented C and C++, and Object Oriented C# and Java) but I'm having a hard time grasping header files.
I have written a small object oriented "hello world" program in C#, and I'm hoping someone here would translate it to the coresponding object oriented c++ code, with header files and such, as it would help me a lot, in understanding the concept.
Thanks a lot in advance!
- Stephen

my code is:
// Name.cs
using System;
namespace ConsoleApp
{
class Name
{
private String myName = "nothing";
public Name() {}
public Name(String newName)
{
this.myName = newName;
}
public String getName()
{
return myName;
}
}
}

// Program.cs
using System;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
Name myName = new Name("Stephen");
Console.Out.WriteLine("Hello "+myName.getName()+"!");
}
}
}

This conversation is currently closed to new comments.

1 total post (Page 1 of 1)  
Thread display: Collapse - | Expand +

All Answers

Collapse -

Figured it out

by The_Parasite In reply to Learning Object Oriented ...

I figured it out, so wanted to give my result, in case anyone else could use it.
- Stephen
my code:
//Hello.h
#include <iostream>
namespace ConsoleApp
{
namespace Hello
{
// begin class
class Name
{
public:
// empty constructor
Name()
{
myName = "Nothing";
}
// constructor
Name(std::string _myName)
{
myName = _myName;
}
// destructor
~Name()
{
myName = "";
}
// getter
std::string getName()
{
return myName;
}
protected:
private:
std::string myName;
// end class
};
}
}

// main.cpp
#include "Hello.h"
using namespace std;
using namespace ConsoleApp::Hello;
int main(int argc, char* argv[])
{
Name emptyName = Name::Name();
Name myName = Name::Name("Steffen");
cout<<"hello "<<emptyName.getName()<<endl;
cout<<"hello "<<myName.getName()<<endl;
emptyName.~Name();
myName.~Name();
return 0;
}

Back to Software Forum
1 total post (Page 1 of 1)  

Related Discussions

Related Forums