Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace SocialMedia
- {
- class Program
- {
- static void Main(string[] args)
- {
- Post post1 = new Post("Thanks for the Birthday Wishes", true, "Jason");
- Console.WriteLine(post1.ToString());
- ImagePost imagePost1 = new ImagePost("Check out my new shoes", "Jason",
- "https://images.com/newshoes.jpg", true);
- Console.WriteLine(imagePost1);
- Console.ReadLine();
- }
- }
- class Post
- {
- private static int currentPostId;
- // properties
- protected int ID { get; set; }
- protected string Title { get; set; }
- public string SendByUserName { get; set; }
- public bool IsPublic { get; set; }
- // Default constructor. If a derived class does not invoke a base class constructor
- // explicitly, the default constructor is called implicitly.
- public Post()
- {
- ID = 0;
- Title = "My First Post";
- IsPublic = true;
- SendByUserName = "Jason Young";
- }
- // Instance constructor that has three parameters
- public Post(string title, bool isPublic, string sendByUsername)
- {
- this.ID = GetNextID();
- this.Title = title;
- this.SendByUserName = sendByUsername;
- this.IsPublic = isPublic;
- }
- protected int GetNextID()
- {
- return ++currentPostId;
- }
- public void Update(string title, bool isPublic)
- {
- this.Title = title;
- this.IsPublic = isPublic;
- }
- //Virtual method override of the ToString method that is inherited from System.Object
- public override string ToString()
- {
- return String.Format("{0} - {1} - by {2}", this.ID, this.Title, this.SendByUserName);
- }
- }
- //ImagePost derives form Post and adds a property (ImageURL) and two constructors
- class ImagePost:Post
- {
- public string ImageURL { get; set; }
- public ImagePost() { } // Constructor
- public ImagePost(string title, string sendByUsername, string imageURL, bool isPublic)
- {
- this.ID = GetNextID(); // these methods are inherited from the Post class
- this.Title = title;
- this.SendByUserName = sendByUsername;
- this.IsPublic = isPublic;
- this.ImageURL = imageURL; // this method is unique to ImagePost class
- }
- //Virtual method override of the ToString method that is inherited from System.Object
- public override string ToString()
- {
- return String.Format("{0} - {1} - {2} - by {3}", this.ID, this.Title, this.ImageURL, this.SendByUserName);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement