Question

In: Computer Science

5. Assignment Instructions: Please respond with at least 100 words. Your initial post should address each...

5.

Assignment Instructions:

Please respond with at least 100 words. Your initial post should address each of the topic below thoughtfully.

Topic: With the advent of object-oriented programming, is it ever necessary to use C-type structs rather than classes? If so, when? What are the advantages or disadvantages of each approach?

Solutions

Expert Solution

In Object Oriented Programming, 

Structure is the same as a Class except for a few differences.

1)The most important of them is security. Structure is not secure and cannot hide its implementation details from the end user while a class is secure and can hide its programming and designing details it means Members of a class are private by default and members of a struct are public by default. Please see below Example in case of security:

class Test {

    int x; // x is private

};

int main()

{

  Test t;

  t.x = 20; // compiler error because x is private

  getchar();

  return 0;

}
======================================================================

struct Test {

    int x; // x is public

};

int main()

{

  Test t;

  t.x = 20; // works fine because x is public

  getchar();

  return 0;

}
========================================================================

2) When deriving a struct from a class/struct, default access-specifier for a base class/struct is public. And when deriving a class, default access specifier is private

class Base {

public:

    int x;

};

  

class Derived : Base { }; // is equilalent to class Derived : private Base {}

  

int main()

{

  Derived d;

  d.x = 20; // compiler error becuase inheritance is private

  getchar();

  return 0;

}
=====================================================================

class Base {

public:

    int x;

};

  

struct Derived : Base { }; // is equilalent to struct Derived : public Base {}

  

int main()

{

  Derived d;

  d.x = 20; // works fine becuase inheritance is public

  getchar();

  return 0;

}
========================================================================

3)Class can create a subclass that will inherit parent's properties and methods, whereas Structure does not support the inheritance i.e. Class can be a Parent Class and derived Classes can inherit this Class in Inheritance but Structure cannot be a Parent Class so it won't be a part of Inheritanc

4)Structs are best suited for small data structures that contain primarily data that is not intended to be modified after the struct is created

5)Structs is created on stack while Class is on Heap so it is faster to instantiate (and destroy) a struct than a class

Related Solutions

Assignment Instructions: Please respond with at least 100 words. Your initial post should address each of...
Assignment Instructions: Please respond with at least 100 words. Your initial post should address each of the topic below thoughtfully. Topic: Many companies use internal programs that were implemented before object-oriented programming came into widespread use. What are the issues involved in moving their systems to an object-oriented approach?
6. Assignment Instructions: Please respond with at least 100 words. Your initial post should address each...
6. Assignment Instructions: Please respond with at least 100 words. Your initial post should address each of the topic below thoughtfully. Topic: Do you think there might be a way to directly access commonly used operators, such as the arithmetic operators or the stream operators, with enumeration types. For example, would overloading these operators be a good solution? Why or why not?
8. Assignment Instructions: Please respond with at least 100 words. Your initial post should address each...
8. Assignment Instructions: Please respond with at least 100 words. Your initial post should address each of the topic below thoughtfully. Topic: What do you think some of the relevant issues to consider when determining whether to use protected or private members in a base class. Are there any risks involved with using the protected member access specifier instead of the private member access specifier?
4. Assignment Instructions: Please respond with at least 100 words. Your initial post should address each...
4. Assignment Instructions: Please respond with at least 100 words. Your initial post should address each of the topic below thoughtfully. Topic: What are some ways to prevent out of bounds errors when reading input into C-strings?
please respond to this assignment with a post of a minimum of 400 words. According to...
please respond to this assignment with a post of a minimum of 400 words. According to SHRM, looking at where recruiters sourced their recent hires underscores the value of healthy social media life. About two-thirds of organizations found new employees through social media, mainly LinkedIn and professional or association networking channels. What will companies need to do in order to actively use the Internet as a recruiting tool as they compete for qualified applicants? In addition to LinkedIn, what other...
Please respond to the discussion post 1 and 2 in your own words: (Must respond to...
Please respond to the discussion post 1 and 2 in your own words: (Must respond to both) Discussion post 1 How do you think most people see the majority of human interactions, as opportunities to compete or to cooperate? I believe that the majority of human interactions are seen as opportunities to compete. The reason I say this is the competitive nature that we have in us as humans. This nature is exemplified daily in our school and our work....
Please respond to the discussion post 1 and 2 in your own words: (Must respond to...
Please respond to the discussion post 1 and 2 in your own words: (Must respond to both) Discussion post 1 Organize and review all your work-to-date. What stands out to you that is potentially of strategic relevance? Think in particular about how your organization is, or can be, or should be different!How will being (more) different in this way potentially create competitive advantages for the organization? Is this a matter of completely reinventing the organization or of enabling it to...
Address all of the following as your Main Post, using at least 250 words: What is...
Address all of the following as your Main Post, using at least 250 words: What is a job order cost system? Describe how you think a job order cost system might be used for professional service businesses. Describe factory overhead costs and give one example of an overhead cost. Lastly, write about one definition or additional concept related to job order costing
In 2-3 paragraphs, respond to the following. Your initial post should be substantive and be supported...
In 2-3 paragraphs, respond to the following. Your initial post should be substantive and be supported by course concepts. Your initial post is due by Wednesday to allow other students to respond. Should the United States government be required to have a balanced budget? What are the long-term implications of continued government indebtedness? cite references.
In 2-3 paragraphs, respond to the following. Your initial post should be substantive and be supported...
In 2-3 paragraphs, respond to the following. Your initial post should be substantive and be supported by course concepts. Your initial post is due by Wednesday to allow other students to respond. How will an understanding of macroeconomics affect your daily life?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT