n
Idioms and Patterns Story
Hello Objects
Home  Repo
Top, Bottom

Hello Objects

Demonstration compares C#, C++, and Rust programs that create simple objects.


Each item in the IdiomsAndPatterns repository focues on a simple program construct built in each of the three languages: C++ 20, Rust 1.49, and C# 9.0. This may help you use what you know of C++ or C# (or Java) to learn Rust more easily. We start here with declaring, defining, and using instances of classes and structs. Find instructions for downloading build chains for each of these languages in Tooling. I've experimented, below, with code colors. Haven't decided which I like best.
C++ hide // CppObject::CreateObj.cpp #include <iostream> #include <string> class DemoObject { public: DemoObject(const std::string& name) : name_(name) {} std::string name(); private: std::string name_; }; std::string DemoObject::name() { return name_; } int main() { DemoObject dob("Zing"); std::cout << "\n Created DemoObject instance with name " << dob.name(); /* create clone using compiler gen copy ctor */ DemoObject cln = dob; std::cout << "\n Created DemoObject clone with name " << cln.name(); std::cout << "\n That's all Folks" << "\n\n"; } C++ build and execute C:\...\CreateObject\CppObject> cd build C:\...\CreateObject\CppObject\build> cmake .. >NULL C:\...\CreateObject\CppObject\build> cmake --build . >NULL C:\...\CreateObject\CppObject\build> "./debug/CreateObj.exe" Created DemoObject instance with name Zing Created DemoObject clone with name Zing That's all Folks Rust unhide // CreateObject::main.rs /*------------------------------------- - Declare DemoObj struct, like C++ class - Rqst compiler impl traits Debug & Clone */ #[derive(Debug, Clone)] pub struct DemoObj { name : String } /*-- implement functions new and name --*/ impl DemoObj { pub fn new(obj_name: &str) -> DemoObj { DemoObj { name: obj_name.to_string() } } pub fn name(&self) -> &str { &self.name } } /*-- Demo DemoObj instance in action --*/ fn main() { print!( "\n -- demonstrate object creation --\n" ); let dob = DemoObj::new("Frank"); print!( "\n DemoObj instance name is {:?}", dob.name() ); let cdob = dob.clone(); print!( "\n name of clone is {:?}", cdob.name() ); print!("\n\n That's all Folks!\n\n"); } Rust build and execute C:\...\CreateObject\RustObject> cargo run -q -- demonstrate object creation -- DemoObj instance name is "Frank" name of clone is "Frank" That's all Folks! C# hide using System; // CSharpObject::Program.cs using System; namespace CSharpObject { public class DemoObject { public DemoObject(String name) { name_ = name; } public String name() { return name_; } public DemoObject clone() { DemoObject cln = new DemoObject(name_); return cln; } private String name_; } class Program { static void Main(string[] args) { Console.WriteLine( "\n -- demo C# object creation --" ); DemoObject dob = new DemoObject("Ashok"); Console.Write( "\n DemoObject created with name {0}", dob.name() ); DemoObject cln = dob.clone(); Console.Write( "\n clone of DemoObject has name {0}", cln.name() ); Console.Write("\n\n That's all Folks!\n\n"); } } } C# build and execute C:\..\CreateObject\CSharpObject> dotnet run /verbosity:quiet -- demonstrate C# object creation -- DemoObject created with name Ashok clone of DemoObject has name Ashok That's all Folks!

4. Epilogue

The following pages provide sequences of code examples for idioms and principles in each of the three languages cited here, e.g. C#, C++, and Rust. Object model differences will often be pointed out in comments within the code blocks.