Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9230845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:55:08+00:00 2026-06-18T05:55:08+00:00

The following is a C++ code for the constructor of linear_program class I have

  • 0

The following is a C++ code for the constructor of linear_program class I have created. The problem is that I have a feeling that, by design, I should overload the >> operator for this class instead of just using >> it in constructor for the class. But then I have to allocate memory dynamically which depends on input taken so I canno segregate the logic completely and even if I overload the operator then I will not be able to take all input at once. That is why I do not see a benefit in overloading >> in this case.

linear_program::linear_program() {
    cin >> dim >> no_constr;  
    lp = new plane[no_constr];
    double *temp = new double [dim];
    double constant;
    for (int i = 0; i < no_constr; ++i) {
            for (int j = 0; j < dim;++j) {
                    cin >> temp[j];
            }
            cin >> constant;
            lp[i].set_plane(temp, constant, dim);
    }
    for (int i = 0; i < no_constr; ++i) {
            cin >> cost[i];
    }
}

Is this acceptable by design standards. I would also like to know if there are other healthy alternatives for such cases.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T05:55:09+00:00Added an answer on June 18, 2026 at 5:55 am

    Depends on what you mean by ‘fine’. But I would suggest to keep object initializations in a constructor, and move the business logic(that does not correspond to creation of that object) to another function.

    A constructor should initialize the object, nothing more.

    linear_program::linear_program(int dim, int no_constr):
     m_noConstr(no_constr), m_Dim(dim)
      {
        lp = new plane[no_constr];
        double constant;
      }
    
    void linear_program::get_something()
    {
       double *temp = new double [m_Dim];
       for (int i = 0; i < m_noConstr; ++i) {
                for (int j = 0; j < m_Dim;++j) {
                        cin >> temp[j];
                }
                cin >> constant;
                lp[i].set_plane(temp, constant, dim);
        }
        for (int i = 0; i < no_constr; ++i) {
                cin >> cost[i];
        }
    }
    
    //Call get_something() after the object has been initialized. It makes reading the code easier. 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following CoffeeScript code: class Person secret = 0 constructor: (@name, @age,
I have the following code as the constructor of a ViewModel class: [ImportingConstructor] public
Consider the following code: The code public class RecursiveConstructor { //When this constructor is
I have the following code: class Number number = null constructor: (num) -> number
This is the constructor of my class . With the following code : public
I have the following code: class Animal constructor: (@name) -> say: () -> console.log
I need the following code to have a default constructor of BalancedString that initializes
In the constructor of an object I have the following code: ConfigReader::ConfigReader(){ TiXmlDocument doc(CONFIGURATION_FILE_LOCATION);
I have the following code: Some functions: A::A(int i_a) {cout<<int Ctor\n;} //conversion constructor void
I'm dealing with a large code base that uses the following construct throughout class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.